我们如何恢复失败的上传? -DropZone.JS

时间:2018-08-01 20:49:03

标签: javascript jquery dropzone.js

我使用DropzoneJs-https://github.com/enyo/dropzone 我想自动恢复失败的上传。

我检查了此问题,对此有解决方案

https://github.com/enyo/dropzone/issues/1150#issuecomment-253480122

这是我现有的Dropzone配置(我尝试添加代码,但无法成功)

var total_photos_counter = 0;
Dropzone.options.myDropzone = {
    uploadMultiple: true,
    parallelUploads: 1,
    maxFilesize: 100,
    previewTemplate: document.querySelector('#preview').innerHTML,
    addRemoveLinks: true,
    dictRemoveFile: 'Resmi Sil',
    dictFileTooBig: 'Dosya 100 MB den büyük. Daha küçük boyutlu bir fotoğraf yükleyiniz' ,
    acceptedFiles: '.jpeg,.jpg,.png,.zip',
    dictCancelUpload: 'Yüklemeyi İptal Et',
    dictInvalidFileType: "Bu tip bir dosyayı yükleyemezsiniz. Sadece resim ve Zip yükleyebilirsiniz.",
    timeout: 100000000,



    init: function () {
        this.on("removedfile", function (file) {
            $.post({
                url: '/images-delete',
                data: {id: file.name, _token: $('[name="_token"]').val()},
                dataType: 'json',
                success: function (data) {
                    total_photos_counter--;
                    $("#counter").text("# " + total_photos_counter);
                }
            });
        });
    },
    success: function (file, done) {
        total_photos_counter++;
        $("#counter").text("# " + total_photos_counter);
    },
    error: function (file,response) {
        var dropzoneFilesCopy = dropzone.files.slice(0);
        dropzone.removeAllFiles();
        $.each(dropzoneFilesCopy, function(_, file) {
            if (file.status === Dropzone.ERROR) {
                file.status = undefined;
                file.accepted = undefined;
            }
            dropzone.addFile(file);
        });
    }
};

如何将这个解决方案添加到我的配置js中。仅仅添加到文件末尾对我来说没有任何意义。

var dropzoneFilesCopy = dropzone.files.slice(0);
        dropzone.removeAllFiles();
        $.each(dropzoneFilesCopy, function(_, file) {
            if (file.status === Dropzone.ERROR) {
                file.status = undefined;
                file.accepted = undefined;
            }
            dropzone.addFile(file);
        });

1 个答案:

答案 0 :(得分:0)

您可以在Dropzone配置的init部分中使用它,并在errormultiple内部使用init事件,如下所示

init: function () {
        this.on("removedfile", function (file) {
            $.post({
                url: '/images-delete',
                data: {id: file.name, _token: $('[name="_token"]').val()},
                dataType: 'json',
                success: function (data) {
                    total_photos_counter--;
                    $("#counter").text("# " + total_photos_counter);
                }
            });
        });

        this.on('errormultiple',function(files, response){
            var dropzoneFilesCopy = files.slice(0);
            myDropzone.removeAllFiles();
            $.each(dropzoneFilesCopy, function(_, file) {
               if (file.status === Dropzone.ERROR) {
                    file.status = undefined;
                    file.accepted = undefined;
               }
               myDropzone.addFile(file);
           });
        });
    },

注意:您需要在正在使用的脚本内相应地更改变量。