javascript – DropZone在init中替换图像
作者:互联网
我用这段代码在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
}
});
解决方法:
我假设您想要在成功上传新图像时替换Dropzone中的旧图像.您可以通过组合Dropzone提供的this.on(‘success’)和this.removeFile方法来实现这一点:
Dropzone.options.myDropzone = {
init: function() {
this.on('success', function(file, message) {
if (this.files.length > 1) {
this.removeFile(this.files[0]);
}
});
// ...
}
};
为此,您的mockFile还必须注册为Dropzone的一个文件.要做到这一点,你可以在显示它之后立即将它推入init中的this.files数组:
// 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#SuccessEvent,
Dropzone#Methods和经验
标签:jquery,javascript,dropzone-js 来源: https://codeday.me/bug/20190624/1276986.html