jquery 上传失败后重试上传相同的文件?- DropZone.JS

qyzbxkaa  于 2023-03-29  发布在  jQuery
关注(0)|答案(1)|浏览(123)

我使用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);
        });
li9yvcax

li9yvcax1#

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

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);
           });
        });
    },

注意:你需要在你正在使用的脚本中相应地改变变量。

相关问题