考虑下面的代码片段的onclick存储相同的值多次
[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的问题,但我仍然有同样的问题
这正是我使用的问题已修复 – Code