【问题标题】:How to delete file in dropzone?如何删除dropzone中的文件?
【发布时间】:2017-02-12 14:29:51
【问题描述】:
      init: function() {
            dzClosure = this;

            document.getElementById("place-order").addEventListener("click", function(e) {
                e.preventDefault();
                e.stopPropagation();
                dzClosure.processQueue();
            });

            this.on("sendingmultiple", function(data, xhr, formData) {
                formData.append("key", $scope.formData.order_id);
            });

            this.on('success', function(file, resp) {
                console.log(resp); //result - {error:false, file_id:10}
                file_ids.push(resp.file_id);
            });
        },
        removedfile: function(file) {
            console.log(file_ids);
            x = confirm('Do you want to delete?');
            if (!x) return false;
            var name = file.name;
            $.ajax({
                type: 'POST',
                url: 'orders/fileDelete.php',
                data: {"file_id": file_ids},
                dataType: 'json'
            });
            var _ref;
            return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;
        }

以上我的代码工作正常。但是我想在单击 dropzone 中的“删除”按钮时删除我的 mysql 行。我无法在我的removedfile 函数中获取当前的file_id。请帮助我,让我知道如何在我的removedfile 函数中获得resp.file_id

【问题讨论】:

    标签: angularjs dropzone.js


    【解决方案1】:

    您可以将 id 属性设置为成功事件的文件,然后在删除时将其作为 file.id 获取。希望对您有所帮助。

    init: function() {
            dzClosure = this;
    
            document.getElementById("place-order").addEventListener("click", function(e) {
                e.preventDefault();
                e.stopPropagation();
                dzClosure.processQueue();
            });
    
            this.on("sendingmultiple", function(data, xhr, formData) {
                formData.append("key", $scope.formData.order_id);
            });
    
            this.on('success', function(file, resp) {
                file.id = resp.file_id;
            });
        },
        removedfile: function(file) {
            x = confirm('Do you want to delete?');
            if (!x) return false;
            //send delete to backend only if file was uploaded.
            //Dropzone will cancel requests in progress itself.
            if(file.id) {
                $.ajax({
                type: 'POST',
                url: 'orders/fileDelete.php',
                data: {"file_id": file.id},
                dataType: 'json'
            });
            }
        }
    

    【讨论】:

    • 我发现了我的错误。我返回了简单的字符串。所以我的file_id 工作不正常。这里我改了代码var response = JSON.parse(resp); file.file_id = response.file_id;谢谢你的尝试。
    【解决方案2】:

    经过大量研究,我发现我的代码哪里出错了。

    实际上我的 ajax 响应了 JSON。但这里的 dropzone.js 没有获取 json 数据。所以我已经将我的动态字符串数据转换为 JSON 格式。

    代码:

    this.on('success', function(file, resp) {
        console.log(resp); // result - {error:false, file_id:10}
        var response = JSON.parse(resp);
        file.file_id = response.file_id;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-13
      • 1970-01-01
      • 2017-08-21
      相关资源
      最近更新 更多