【问题标题】:How can I stop dropzone from removing my file?如何阻止 dropzone 删除我的文件?
【发布时间】:2026-02-07 07:35:01
【问题描述】:

我为 dropzone 配置了“removedfile”事件。
但在函数中,我从服务器中删除了文件。

如果服务器删除文件失败,
然后我希望 dropzone 事件取消从 dropzone 元素窗口中删除文件。

这可能吗?

这是我的删除代码

this.on('removedfile', function(file) {
  //remove file from server
  if (file.status == 'success') {
    var model = {
      contractId: contractId,
      clientId: scope.currentClientId,
      fileName: file.name
    };
    scope.ajaxPost(scope.enumControllers.deleteContractAttachment, model, function(response) {
      response.error == false ? tp.showAlert(response.Message, 'success') : tp.showAlert(response.Message, 'error');;
    });
  }
});

【问题讨论】:

    标签: javascript jquery dropzone.js


    【解决方案1】:

    我遇到了问题,我解决了。
    你需要修改源代码。

    1:覆盖removedfile 事件。为 Dropzone 选项添加删除功能,如下所示:

    new Dropzone({
          // options
          removefile: function(){
              // remove from server...
          }
    });
    

    2:在removeFile 函数中: 删除此代码:this.files = without(this.files, file);

    3:将without移动到dropzone.prototype

    Dropzone.prototype.without = function(list, rejectedItem) {//...}
    

    最后:这是我的 removedfile 函数(它不是 init 函数的事件,而是 dropzone 选项的一个选项):

    removedfile: function (file) {
                    var _ref,
                        isFileUploadSuccess = (file.status === Dropzone.SUCCESS),
                        isDeleteSuccess;
                    if (isFileUploadSuccess) {
                        customOptions && typeof customOptions.removeFunc === "function" && (isDeleteSuccess = customOptions.removeFunc(file));
                    }
                    if ((isFileUploadSuccess && isDeleteSuccess || !isFileUploadSuccess) && file.previewElement && (_ref = file.previewElement) !== null) {
                        _ref.parentNode.removeChild(file.previewElement);
                        this.files = this.without(this.files, file);
                    }
                    return this._updateMaxFilesReachedClass();
                }
    

    removeFunc 是我通过 ajax (async:false) 从服务器中删除文件的功能,所以无论文件是否成功删除,我都可以收到结果;

    也许我的代码不太好,但它解决了我的问题。

    【讨论】: