【问题标题】:How can we resume failed uploads? - DropZone.JS我们如何恢复失败的上传? - DropZone.JS
【发布时间】:2018-08-01 20:49:02
【问题描述】:

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

【问题讨论】:

  • init 部分中使用它,如this.on("errormultiple", function (files, response) { 我从未尝试过,但它应该在那里工作
  • 能否请您分享作为答案,以便我批准。
  • 你最后试过了吗?
  • 如果对你有用,请标记答案

标签: javascript jquery dropzone.js


【解决方案1】:

您可以在 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);
           });
        });
    },

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

【讨论】:

  • 问题是我的系统没有启用多重上传。我一一上传。我仍然应该使用errormultiple吗?这样如果一个文件失败了,它会重试,它不会等待其他人完成。
  • 您正在使用uploadMultiple:true 看到配置中的代码,我有点困惑。但如果您不上传多个文件,您可以使用error,我已经更新了代码。 @SNaRe
  • 我尝试了多个版本和 1by1 版本。它不起作用。多个对我来说也是可以接受的,但效果不佳。如果我忘记更改脚本中的一些变量,请检查我的配置。请再次将其转换为多个。无论如何,这更合乎逻辑。
  • 当你说不工作时,你能更具体一点吗?控制台上的任何错误或任何东西。我已经更新了答案,请再次复制脚本,我已经更改了 . dropzonemyDropzone,因为实例保存在 myDropzone var 中,而不是未定义的 dropzone,并且会在 dropzone.addFiles()dropzone.removeAllFiles() @SNaRe 上引发错误
  • 我假设您将 dropzone 的实例保存在 var myDropZone
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-04
  • 1970-01-01
  • 2015-04-08
  • 2019-04-11
  • 1970-01-01
  • 2021-12-15
  • 2015-06-05
相关资源
最近更新 更多