Dropzone具有正常形式

时间:2014-03-06 08:06:40

标签: javascript php ajax forms dropzone.js

我的问题是我必须将普通表单与dropzone.js结合使用才能进行拖放上传。用户单击提交按钮后,如果输入中有值,则ajax-request将数据发送到php脚本。

但我如何通过dropzone和ajax-request组合文件?当用户点击按钮时,我会发送两个。如果我在区域中拖动文件而不是文件将被发送。

autoProcessQueue: false

这使得,如果用户在区域中拖动文件,则不会发送文件。

需要解决方案:用户填写表单,拖动区域中的文件,如果用户单击按钮,则将使用ajax-request发送值和文件。

代码的一些演示: http://jsfiddle.net/wQP5B/

1 个答案:

答案 0 :(得分:10)

我也有同样的问题,但我解决了。 您可以从dropzone结帐此链接 - > https://github.com/enyo/dropzone/wiki/Combine-normal-form-with-Dropzone

他们已经给出了你想要的东西但是在他们给出的东西中有一些东西需要添加,比如没有使整个形式可点击和其他东西。 以下代码适用于我

<强> form.html

<form method="post" action="upload.php" class="dropzone" id="mydropzone" enctype='multipart/form-data'> //remember we gave an id mydropzone to the form         
    <label>Username:<input type="text" name="uname"/> </label>
    <label>Password:<input type="text" name="pass"/> </label>
    //this will the dropzone drag and drop section.
    //notice we have given it an id dropzonePreview.
    <div id="dropzonePreview"></div>
    <input type="button" id="sbmtbtn" value="submit"/>
    //we have given id sbmtbtn to the button   
</form>
<script>
    Dropzone.options.mydropzone = {
    //url does not has to be written 
    //if we have wrote action in the form 
    //tag but i have mentioned here just for convenience sake 
        url: 'upload.php', 
        addRemoveLinks: true,
        autoProcessQueue: false, // this is important as you dont want form to be submitted unless you have clicked the submit button
        autoDiscover: false,
        paramName: 'pic', // this is optional Like this one will get accessed in php by writing $_FILE['pic'] // if you dont specify it then bydefault it taked 'file' as paramName eg: $_FILE['file'] 
        previewsContainer: '#dropzonePreview', // we specify on which div id we must show the files
        clickable: false, // this tells that the dropzone will not be clickable . we have to do it because v dont want the whole form to be clickable 
        accept: function(file, done) {
            console.log("uploaded");
            done();
        },
        error: function(file, msg){
            alert(msg);
        },
        init: function() {
            var myDropzone = this;
            //now we will submit the form when the button is clicked
            $("#sbmtbtn").on('click',function(e) {
               e.preventDefault();
               myDropzone.processQueue(); // this will submit your form to the specified action path
              // after this, your whole form will get submitted with all the inputs + your files and the php code will remain as usual 
        //REMEMBER you DON'T have to call ajax or anything by yourself, dropzone will take care of that
            });      
        } // init end
    };
</script>

注意:你不必在php文件中做任何花哨的东西。只需编写您通常用PHP编写的内容来上传文件和输入。

看看这是否适合你。