DropZone在init中替换图像

时间:2014-10-12 07:37:59

标签: 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
            }

        });

1 个答案:

答案 0 :(得分:4)

我假设您想在成功上传新图片时替换Dropzone中的旧图片。您可以通过组合this.on('success')提供的this.removeFileDropzone方法来实现这一目标:

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

        // ...
    }
};

为此,您的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];

<强> 来源: Dropzone#SuccessEventDropzone#Methods和经验