gpt4 book ai didi

javascript - 在 Dropzone 中添加现有图像文件

转载 作者:行者123 更新时间:2023-12-03 02:03:06 27 4
gpt4 key购买 nike

我正在使用 Dropzonejs 在表单中添加图像上传功能,因为表单中还有各种其他字段,因此我将 autoProcessQueue 设置为 false 并单击表单的“提交”按钮进行处理,如下所示。

Dropzone.options.portfolioForm = { 

url: "/user/portfolio/save",
previewsContainer: ".dropzone-previews",
uploadMultiple: true,
parallelUploads: 8,
autoProcessQueue: false,
autoDiscover: false,
addRemoveLinks: true,
maxFiles: 8,

init: function() {

var myDropzone = this;
this.element.querySelector("button[type=submit]").addEventListener("click", function(e) {

e.preventDefault();
e.stopPropagation();
myDropzone.processQueue();
});
}
}

这工作正常,允许我处理提交表单时发送的所有图像。但是,我还希望在用户再次编辑表单时能够看到用户已经上传的图像。所以我浏览了 Dropzone Wiki 上的以下帖子。 https://github.com/enyo/dropzone/wiki/FAQ#how-to-show-files-already-stored-on-server

它使用现有图像填充 dropzone-preview 区域,但这次不会通过表单提交发送现有图像。我想这是因为这些图像没有添加到队列中,但如果是这样,那么如何在服务器端更新,以防用户删除现有图像?

另外,更好的方法是什么,再次将已添加的图像添加到队列中,还是仅发送已删除文件的信息?

最佳答案

我花了一些时间尝试再次添加图像,但经过一段时间的斗争后,我最终只是将有关已删除图像的信息发送回服务器。

当使用现有图像填充 dropzone 时,我将图像的 url 附加到 mockFile 对象。在removedfile事件中,如果要删除的文件是预先填充的图像(通过测试传递到事件中的文件是否具有url属性来确定),我会向表单添加隐藏输入。我已包含以下相关代码:

Dropzone.options.imageDropzone = {
paramName: 'NewImages',
autoProcessQueue: false,
uploadMultiple: true,
parallelUploads: 100,
maxFiles: 100,
init: function () {
var myDropzone = this;

//Populate any existing thumbnails
if (thumbnailUrls) {
for (var i = 0; i < thumbnailUrls.length; i++) {
var mockFile = {
name: "myimage.jpg",
size: 12345,
type: 'image/jpeg',
status: Dropzone.ADDED,
url: thumbnailUrls[i]
};

// Call the default addedfile event handler
myDropzone.emit("addedfile", mockFile);

// And optionally show the thumbnail of the file:
myDropzone.emit("thumbnail", mockFile, thumbnailUrls[i]);

myDropzone.files.push(mockFile);
}
}

this.on("removedfile", function (file) {
// Only files that have been programmatically added should
// have a url property.
if (file.url && file.url.trim().length > 0) {
$("<input type='hidden'>").attr({
id: 'DeletedImageUrls',
name: 'DeletedImageUrls'
}).val(file.url).appendTo('#image-form');
}
});
}
});

服务器代码(asp mvc Controller ):

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(ViewModel viewModel)
{
if (ModelState.IsValid)
{
foreach (var url in viewModel.DeletedImageUrls)
{
// Code to remove the image
}

foreach (var image in viewModel.NewImages)
{
// Code to add the image
}
}
}

希望对您有所帮助。

关于javascript - 在 Dropzone 中添加现有图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24445724/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com