Dropzone功能成功

时间:2016-03-10 08:56:14

标签: javascript jquery html dropzone.js

我有这段代码

CODE JS

var addedFiles = [];

    Dropzone.autoDiscover = false;
    var myDropzone = new Dropzone("div#myDrop", {
        url             : "<?php echo $this->serverUrl() . '/profile/ajax/dzupload'; ?>",
        paramName       : 'patientfile',
        acceptedFiles   : "image/*",
        maxFiles        : 10,
        parallelUploads : 10,
        addRemoveLinks  : true,
        autoProcessQueue: false,
        uploadMultiple  : true,

        accept : function(file, done){
            // we reject files with the same name on the same upload
            if($.inArray(file.name,addedFiles) >= 0 ) {
                // if file is already in the list we return done() with a message in order to
                // notify the user that he cannot upload it and also prevent if from uploading
                done("File with the same name cannot be uploaded");
            } else {
                // if the file is ok, we add it it's name to the list for further validation
                addedFiles.push(file.name);
                return done();
            }

        },

        init : function() {
            this.on("addedfile", function(file) {
                //console.log(this.addedFiles);
            });

            // on click on either of the two "Save" buttons we send the files to be processed on backend
            $('[id^=savePatientFiles_]').click(function(e){
                e.preventDefault();
                myDropzone.processQueue();
            });

            this.on('sending', function(file, xhr, formData){
                formData.append("patientId", "<?php echo $this->pacientInfo->id ?>");
            });

            this.on("success", function(file, responseText) {
                // from backend a response will be returned for every file uploaded 

                alert("test");   //This alert is performed twice for each added file

                // console.log(file);
                var responseSuccess = [];
                // console.log(responseText);
                $.each(responseText, function( index, value ) {
                //  console.log(value.succes);
                    responseSuccess.push(value.success );
                    console.log(responseText);
                //  window.location.href = 'your_url';
                });

                if ($.inArray(false,responseSuccess) == -1){
                    var url_redirect = "<?php echo $this->serverUrl().str_replace('public','',$this->basePath()).'/user/viewfilesforpatient/'.$this->pacientInfo->id; ?>"
                    window.location.href = url_redirect;
                    // TODO: if false is not returned, redirect the user to the page where he can see the files
                } else {

                    // TODO: append error messages inside the page to warn the user what was wrong
                }

            });

            this.on("maxfilesexceeded", function(file){
                this.removeFile(file);
                alert("You are not allowed to chose more than 10 file!");
            });
        }
    });

您能否向我解释为什么每个添加的文件都会执行两次“成功”功能?

例如:

If I upload 1 file --- `alert()`--two executions
If I upload 2 file --- `alert()`--four executions

----- and so on

执行此操作的原因是什么?如何将其限制为每个文件只执行一次?

提前致谢!

修改

http://codepen.io/anon/pen/wGWwMW

我试图伪造我的问题codepen,但我们做了.. 对每个文件执行一次此警报

0 个答案:

没有答案