javascript - 如何阻止 dropzone 删除我的文件?

标签 javascript jquery dropzone.js

我配置了 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');;
    });
  }
});

最佳答案

我遇到了问题并解决了它。
您需要修改源代码。

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) {//...}

last:这是我的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 ) 从服务器删除文件的函数,因此无论文件是否成功删除,我都可以收到结果;

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

关于javascript - 如何阻止 dropzone 删除我的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38000795/

相关文章:

jquery - 使用 PhoneGap/jQuery mobile 将数据发送到本地网络上的 TCP 服务器

javascript - ASP.Net MVC 中的简单 Dropzone 实现 - 如何获取 Controller 中的数据?

javascript - ndb.key() -> 使用 Javascript 执行此操作

javascript - 正则表达式仅用制表符替换字符串

jquery - 如何使用jquery更改链接中的变量值

php - 单击 JavaScript 弹出框链接后激活 jQuery 功能

meteor - 为什么第三方 JavaScript 库没有在 Meteor 中呈现?

javascript - 如何更改 dropzone.js 中的默认文本?

javascript - 预输入更新功能

javascript - 在 Firefox 中更平滑的滚动过渡?