[英]Onclick storing same value multiple times
请考虑以下代码段
[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>
我也遇到过这个问题,但我仍有同样的问题
看起来uploadMultiple选项将改变行为,因此只有一个请求被发送到服务器。
var myDropZone = new Dropzone("#dzUpload", {
url: "/Home/Create",
autoProcessQueue: false,
previewsContainer: ".preview",
uploadMultiple: true,
});
所以,如果我是对的,插件会为你插入插件的每个文件发布表单吗? 一种方法是生成一个GUID并将其保存在表单隐藏输入中。 因此,每次您的插件发布时,它也会发布此GUID。 因此,根据Guid将insert语句更改为upsert(更新或插入)。您必须将此GUID与其他数据一起保存。
所以每次你打算插入检查GUID是否已经存在如果是这样更新它,否则插入新记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.