DropZone 在 init 中替换图像

moh*_*her 4 javascript jquery dropzone.js

我使用此代码从 init 中的数据库上传图像。现在我想在上传新图像时删除这个初始图像。

 var o = $("div#uploader").dropzone({
            url: "../../Merchant/UploadLogo",
            paramName: "logo",
            maxFiles: 1,

            //enqueueForUpload: false,
            maxfilesexceeded: function (file) {
                this.removeAllFiles();
                this.addFile(file);
            },

            addRemoveLinks: true,
            uploadMultiple: false,
            dictRemoveFile: "???",

            removedfile: function(file) {
                RemoveFile("Logo");
                var _ref;
                return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;
            },

            init: function() {

                var mockFile = { name: "avatar1.jpg", size: 12345, type: 'image/jpeg', url: "../../Merchant/GetLogo" };
                this.options.addedfile.call(this, mockFile);
                this.options.thumbnail.call(this, mockFile, mockFile.url);//uploadsfolder is the folder where you have all those uploaded files
            }

        });
Run Code Online (Sandbox Code Playgroud)

She*_*yar 5

我假设您想在Dropzone成功上传新图像时替换 中的旧图像。您可以通过组合this.on('success')this.removeFile提供的方法来做到这一点Dropzone

Dropzone.options.myDropzone = {
    init: function() {
        this.on('success', function(file, message) {
            if (this.files.length > 1) {
                this.removeFile(this.files[0]);
            }
        });

        // ...
    }
};
Run Code Online (Sandbox Code Playgroud)

为此,您mockFile还必须注册为 Dropzone 的文件之一。为此,您可以在显示它之后将其推送到 中的this.files数组中init

// Create and Display Mock File
var mockFile = { name: "avatar1.jpg", size: 12345, url: '/image.png' };
this.options.addedfile.call(this, mockFile);
this.options.thumbnail.call(this, mockFile, mockFile.url);

// Register it
this.files = [mockFile];
Run Code Online (Sandbox Code Playgroud)

来源:Dropzone#SuccessEventDropzone#Methods and Experience