Saving dropzone uploaded files to array

2019-07-30 10:25发布

问题:

I get return of the file names when the files are uploaded, but i want to store all of the file names in array and attach them to php form. This is what i have so far.

  this.on("success", function(file, res) {
        console.log(res.path);
        file.newFileName = res;
      $path = res.path;

       // Create a hidden input to submit to the server:
    $("#image").append($('<input type="hidden" ' +'name="files[]" ' +'value="' + res.path + '">'));

      });

and then i have, this in php form. It works for one filename but only saves most current one.

{!! Form::hidden('image', '', array('id' => 'user-image')) !!}

回答1:

your code is perfect. I don't understand why it isn't working on your side.

this is the sample code ... which is running for me.

        Dropzone.options.myDropzone = {
            paramName: "product_image",
            addRemoveLinks: true,
            success: function (file, response) {
                var obj = JSON.parse(response);
                file.name = obj.file_name;
                if (obj.error == true) {
                    file.previewElement.classList.add("dz-error");
                } else {
                    file.previewElement.classList.add("dz-success");
                    $("#my-dropzone").append($('<input type="hidden" ' + 'name="product_images[]" ' + 'value="' + obj.file_name + '">'));
                }
            }
        };

But this only work if you are uploading images one by one.. else you have to try loop if you are uploading mulitple images at the time. I hope you understand.