javascript 普通形式的 Dropzone

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22218552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:43:02  来源:igfitidea点击:

Dropzone with normal form

javascriptphpajaxformsdropzone.js

提问by Tim Hartmann

My problem is that I must combine a normal form with dropzone.js for a drag&drop upload. After the user clicks the submit-button, then a ajax-request send the data to a php-script if there are values in the inputs.

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

But how I can combine the files by dropzone and the ajax-request? I would send both, when the user clicks the button. If I drag a file in the zone than the file will be send.

但是我如何通过 dropzone 和 ajax 请求组合文件?当用户单击按钮时,我会同时发送两者。如果我在区域中拖动文件,则会发送该文件。

autoProcessQueue: false

This make it, that the file doesn't will be send if the user drag a file in the zone.

这样一来,如果用户在区域中拖动文件,则不会发送该文件。

Solution needed: User fills the form, drag a file in the zone and if the user click on the button, then the values and files will be send with a ajax-request.

需要的解决方案:用户填写表单,在区域中拖动一个文件,如果用户单击按钮,则值和文件将与 ajax 请求一起发送。

Some demo for the code:http://jsfiddle.net/wQP5B/

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

回答by Vishal Nair

I also had the same problem but i solved it. You can checkout this link from dropzone --> https://github.com/enyo/dropzone/wiki/Combine-normal-form-with-Dropzone

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

They have given what you want BUT there are some things to be added in what they have given like not making the whole form clickable and other things . The code below works fine for me

他们提供了您想要的东西,但是在他们提供的东西中还需要添加一些东西,例如不能使整个表单可点击和其他东西。下面的代码对我来说很好用

form.html

表单.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>

NOTE: YOU DONT have to do any fancy stuff in the php file . Just write what you would normally write in PHP to upload files and input .

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

See if this works for you.

看看这是否适合你。