admin管理员组

文章数量:1425043

I used this code to upload image from database in init. Now I want when upload a new image to remove this initial image.

 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
            }

        });

I used this code to upload image from database in init. Now I want when upload a new image to remove this initial image.

 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
            }

        });
Share Improve this question edited Jul 1, 2015 at 9:10 Sheharyar 75.9k21 gold badges174 silver badges223 bronze badges asked Oct 12, 2014 at 7:37 mohammed beshermohammed besher 3785 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

I'm assuming you want to replace the old image in the Dropzone whenever a new Image is successfully uploaded. You can do that by bining the this.on('success') and this.removeFile methods provided by Dropzone:

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

        // ...
    }
};

For this to work, your mockFile must also be registered as one of the files of your Dropzone. To do that, you can push it into the this.files array in your init, right after displaying it:

// 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];

Source: Dropzone#SuccessEvent, Dropzone#Methods and Experience

本文标签: javascriptDropZone Replace image in initStack Overflow