如何删除dropzone中的文件?

How to delete file in dropzone?

      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 中的 "Remove" 按钮时删除我的 mysql 行。我无法在我的 removedfile 函数中获取当前的 file_id。请帮助我,让我知道如何在我的 removedfile 函数中获得 resp.file_id

您可以设置一个 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'
        });
        }
    }

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

实际上我的 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;
});