简体   繁体   English

dropzone.js文件上传遇到问题

[英]Trouble with dropzone.js file upload

I'm using for the first time dropzone.js inside another form...so as two forms can't be nested, I removed dropzone's form: 我第一次在另一种表单中使用dropzone.js ...因此无法嵌套两个表单,因此我删除了dropzone的表单:

html snippet: html片段:

<form id="addproduct" name="addproduct" action="receiveAddForm" method="post" enctype="multipart/form-data">
  <fieldset class="form-horizontal">
    <div class="form-group">
      <div class="col-sm-2">
        <small class="text-navy"><b>* Campos Obligatorios</b></small>
      </div>
    </div>
    <div class="form-group"><label class="col-sm-2 control-label">Nombre (Modelo) *:</label>
      <div class="col-sm-10"><input name="name" type="text" class="form-control"></div>
    </div>
  </fieldset>

  <div class="dropzone dropzone-previews" id="my-awesome-dropzone"></div>
</form>

js snippet: js片段:

Dropzone.options.myAwesomeDropzone = {
        autoProcessQueue: false,
        uploadMultiple: true,
        parallelUploads: 100,
        maxFiles: 3,
        addRemoveLinks: true,
        maxFilesize: 10,
        url: 'receiveAddForm',

        init: function() {
            var myDropzone = this;                

            $("#submit_form").click(function (e) {
                e.preventDefault();
                e.stopPropagation();
                myDropzone.processQueue();
            });

            this.on("sendingmultiple", function() {
            });
            this.on("successmultiple", function(files, response) {
            });
            this.on("errormultiple", function(files, response) {
            });

            this.on("maxfilesexceeded", function(file){
                // alert("No more files please!");
            });

            this.on("uploadprogress", function(file, progress) {
                console.log("File progress", progress);
            });
        }
    }

so in server side I'm getting this after echoing $_FILES: 所以在服务器端,我在回显$ _FILES之后得到了这个:

array (size=1)
'files' => 
  array (size=5)
    'name' => string '' (length=0)
    'type' => string '' (length=0)
    'tmp_name' => string '' (length=0)
    'error' => int 4
    'size' => int 0

what seems to be the problem here? 这里似乎是什么问题? my php.ini is set to 1000MB in upload max file size, memory limit and so on... any help would be appreciatted! 我的php.ini文件的最大上传文件大小,内存限制等设置为1000MB,任何帮助将不胜感激!

The 'error' => int 4 means that no file has been uploaded, i think this is because you are submitting the form like if it was a regular form, if you want to include dropzone inside a regular form i don't think you can submit the form the regular way and attach to it the files dropped in the dropzone element, or at least there is no simple way to do it, one solution could be to encode the file in base64 and then add the encoded string to an input to send. 'error' => int 4表示没有文件上传,我认为这是因为您正在提交表单,就像它是常规表单一样,如果您想在常规表单中包含dropzone我不认为您可以按常规方式提交表单并附加放置在dropzone元素中的文件,或者至少没有简单的方法可以做到这一点,一种解决方案是将文件编码为base64,然后将编码后的字符串添加到输入中发送。

But an easy one I think is to send the form using dropzone and append the input values to the request using javascript, here generic example. 但是我认为一个简单的方法是使用dropzone发送表单,然后使用javascript(此处为通用示例)将输入值附加到请求中。

html: 的HTML:

<form id="addproduct">
    <label>Input 1: </label>
    <input type="text" name="input1">
    <label>Input 2: </label>
    <input type="text" name="input2">
    <div id="myAwesomeDropzone" class="dropzone"></div>
</form>
<button type="button" id="submit_form">Submit</button>

js: js:

Dropzone.options.myAwesomeDropzone = {
    url: 'receiveAddForm',
    autoProcessQueue: false,
    uploadMultiple: true,
    parallelUploads: 3,
    maxFiles: 3,
    init: function () {
        var myDropzone = this;
        $('#submit_form').on("click", function() {
            myDropzone.processQueue();
        });
        this.on("sending", function(file, xhr, formData){
            $('#addproduct').find('input').each(function() {
                formData.append( $(this).attr('name'), $(this).val() );
            });
        });
        this.on("success", function(file, response) {
            console.log(response);
        });
        this.on("completemultiple", function(files) {
            // Here goes what you want to do when the upload is done
            // Redirect, reload , load content ......
        });
    },

};

receiveAddForm (php): receiveAddForm(php):

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') 
{
    echo "RECEIVED ON SERVER: \n";
    print_r($_FILES);
    print_r($_POST);
}

The server file just prints the data received on the server, so you can see it in browsers console. 服务器文件仅打印服务器上接收到的数据,因此您可以在浏览器控制台中看到它。 I omitted bootstrap classes and elements only to show the relevant part, but you cand add them no problem. 我省略了引导程序类和元素只是为了显示相关部分,但是您可以添加它们没有问题。

to have a dropzone inside another form you can put a div in the form with class="dropzone" and convert it to dropzone element like this: 要在另一个表单中放置一个dropzone,您可以将div放在class =“ dropzone”的表单中,并将其转换为dropzone元素,如下所示:

Dropzone.autoDiscover = false;

var myDropzone = new Dropzone("#my-awesome-dropzone", {
  autoProcessQueue: false,
  url: "receiveAddForm",
  // other options
});

then you can call the events like this: 那么您可以像这样调用事件:

myDropzone.on("addedfile", function(file) {
  console.log("File added:", file.name);
});

jsfiddle with your form : fiddle jsfiddle与您的形式: 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM