gpt4 book ai didi

c# - onclick 多次存储相同的值

转载 作者:行者123 更新时间:2023-11-30 17:38:23 25 4
gpt4 key购买 nike

考虑以下代码片段

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(MyViewModel viewModel)
{
if (ModelState.IsValid)
{
//map properties here

using (var context = new MyEntities())
{
context.Users.Add(user);
context.SaveChanges();
}

if (Request.Files.Count > 0)
{
foreach (string fileName in Request.Files)
{
HttpPostedFileBase file = Request.Files[fileName];
if (file != null && file.ContentLength > 0)
{
//do checks and upload file here
}
}
}
}
return RedirectToAction("Index", "Home");
}

表单可以单独提交,也可以与文件一起提交,然后上传到服务器。现在我的问题是,如果我提交的表格没有任何文件或只有一个文件,那么一切都会按预期进行。但是,用户一次可以上传多个文件,这就是问题所在。文件已上传,但我在数据库中获得了该特定表单的多个条目。例如,如果用户上传三个文件,我将在数据库中得到三个完全相同的条目。

所以我的问题是如何解决这个问题?

在客户端,我使用 DropZoneJs 并将方法调用为

<script>
Dropzone.autoDiscover = false;
var myDropZone = new Dropzone("#dzUpload", {
url: "/Home/Create",
autoProcessQueue: false,
previewsContainer: ".preview",
});

$("#submit-all").attr("type", "button").on('click', function (e) {
e.preventDefault();
e.stopPropagation();

if (myDropZone.getQueuedFiles().length > 0) {
myDropZone.options.autoProcessQueue = true;
myDropZone.processQueue();
}
else {
$("#dzUpload").submit();
}
});
</script>

我也遇到过 this 问题,但我仍然有同样的问题

最佳答案

它看起来像 uploadMultiple选项将改变行为,因此只有一个请求被发送到服务器。

var myDropZone = new Dropzone("#dzUpload", {
url: "/Home/Create",
autoProcessQueue: false,
previewsContainer: ".preview",
uploadMultiple: true,
});

关于c# - onclick 多次存储相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36745296/

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