【发布时间】:2019-06-30 19:16:58
【问题描述】:
我知道上传多个文件可以正常工作,因为当我注释掉
[ValidateAntiForgeryToken]我可以选择多个文件,它们将按预期上传而没有任何问题。
但是,当我将[ValidateAntiForgeryToken] 放回If I select 2 or more files 时,我得到了服务器500 status error,并且没有任何文件被上传。
另外,我将添加错误:
Failed to load resource: the server responded with a status of 500 (Internal Server Error) 堆栈跟踪说它停止在 line 1 of Upload action
但是,如果我选择 1 个文件,它会成功上传并获得 status code 200。
我对此还是很陌生 - 我不知道出了什么问题。我感谢任何关于这个谜的帮助。 :-)
这是我的控制器操作:
[HttpPost]
[ValidateAntiForgeryToken] // If I comment this out, everything works as intended
public ActionResult Upload()
{
for (int i = 0; i < Request.Files.Count; i++)
{
var file = Request.Files[i];
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Some/FilePath"), fileName);
file.SaveAs(path);
}
return Json(new { success = true, responseText = "Success!" }, JsonRequestBehavior.AllowGet); //This is placeholder, I'll implement validation later
}
HTML:
@Html.TextBoxFor(model => model.file, new { type = "file", id = "file-upload", multiple="multiple" })
@Html.ValidationMessageFor(model => model.file, "", new { @class = "text-danger" })
<div id="selectedFiles"></div>
我构建了我的自定义文件数组,所以我可以拥有一个花哨的删除文件功能。
这就是我调用UploadAjax 函数的方式:
var storedFiles = []; //this is what I pass to it.
$("#stupidTest").click(function () {
UploadAjax(storedFiles);
});
JQuery、AJAX。这是上传功能。
function UploadAjax(storedFilesArray) {
var formData = new FormData();
for (let i = 0; i < storedFilesArray.length; i++) {
let file = storedFilesArray[i];
formData.append('__RequestVerificationToken', getToken()); //appends the value to the formData.
formData.append("file-upload", file);
}
$.ajax({
type: "POST",
dataType: 'json',
cache: false,
url: '/Home/Upload',
data: formData,
contentType: false,
processData: false,
success: function (response) {
...
},
error: function (response) {
...
}
});
}
**Edit: Found that this happened at the same second my multiple file upload request failed**
System.Web.Mvc.HttpAntiForgeryException: The anti-forgery token could not be
decrypted. If this application is hosted by a Web Farm or cluster, ensure that
all machines are running the same version of ASP.NET Web Pages and that the
<machineKey> configuration specifies explicit encryption and validation keys.
AutoGenerate cannot be used in a cluster.
【问题讨论】:
-
您只得到最后一个文件,因为所有文件都具有相同的文件名。它只是看起来像一个文件被上传(最后一个)。检查上传文件的大小,看看文件夹中的文件大小。
-
对不起,我应该告诉你的。当我使用 [ValidateAntiforgeryToken] 选择多个文件时,我没有上传任何文件。只有当我选择1个文件时,我才会在相同的情况下上传它。我会更新帖子。 :)
标签: c# ajax model-view-controller csrf antiforgerytoken