[英]FormData key does not go through
I use external class for making object variable into FormData variable. 我使用外部类将对象变量转换为FormData变量。 All of the keys go through, all but One for File object.
除了“一个用于文件”对象外,所有键都通过。
Here is that external class: https://github.com/therealparmesh/object-to-formdata 这是该外部类: https : //github.com/therealparmesh/object-to-formdata
This is how I create objects and make them into FormData 这就是我创建对象并将其制成FormData的方式
var _documents = [];
for (var i = 0; i < arrayOfFiles.length; i++) {
var document = {
File: arrayOfFiles[i].file.nativeFile,
DocumentId: arrayOfFiles[i].documentId,
DocumentType: arrayOfFiles[i].documentName
};
_documents.push(document);
}
var uploadedInformation = {
LoanID: 1452465,
documents: _documents
};
var options = {
indices: true,
nulls: true
};
var a = objectToFormData(uploadedInformation, options);
for (var pair of a.entries()) {
console.log(pair[0] + ', ' + pair[1]);
}
jQuery.ajaxSettings.traditional = true;
$.ajax({
async: false,
cache: false,
contentType: false,
processData: false,
type: 'POST',
url: '@Url.Action("UploadFile", "Home")',
data: a
});
Code for controller: 控制器代码:
[HttpPost]
[ActionName("UploadFile")]
public ActionResult UploadFile(UploadedInformation uploadedInformation)
{
_ = Request.Form;
return View();
}
UploadedFile class: UploadedFile类:
public class UploadedInformation
{
public long LoanID { get; set; }
public IEnumerable<Document> Documents { get; set; }
}
Document class: 文件类别:
public class Document
{
public HttpPostedFileBase File { get; set;}
public string DocumentId { get; set;}
public string DocumentType { get; set; }
}
All of the items bind perfectly, except for File
. 除
File
之外,所有项目均绑定完美。 In browser debugger keys and values are: 在浏览器中,调试器的键和值是:
LoanID, 1452465
documents[0][File], [object File]
documents[0][DocumentId], 1
documents[0][DocumentType], Passport
_=Request.Form
also displays only 3 keys without documents[0][File]
_=Request.Form
也仅显示3个没有documents[0][File]
键documents[0][File]
Update: I changed controller to 更新:我将控制器更改为
public ActionResult UploadFile(IEnumerable<HttpPostedFileBase> file, IEnumerable<string> documentType, IEnumerable<string>documentId, long loanId){...}
and _=Request.Form
still shows nothing with file
, however file list is populated 和
_=Request.Form
仍然不显示file
,但是填充了文件列表
Another update: Apparently, this time file items shows only in _=Request.File
另一个更新:显然,这次文件项仅显示在
_=Request.File
Due to the way that the controller is handling the file upload parts of the request, I suspect you may need to make some adjustments to your process and allow for the fact that the files are being separated from the main object. 由于控制器处理请求的文件上载部分的方式,我怀疑您可能需要对过程进行一些调整,并考虑到文件与主对象分离的事实。
I've included some adjustments to your code below (note, this is untested so you may need to experiment a little), assuming that the files come through in the same order as you Documents, then just run a match up process before running your own process. 我在下面对您的代码进行了一些调整(请注意,这未经测试,因此您可能需要进行一些试验),假设文件的处理顺序与您的文档顺序相同,然后在运行您的文档之前运行匹配流程自己的过程。
Code for controller: 控制器代码:
[HttpPost]
[ActionName("UploadFile")]
public ActionResult UploadFile(List<HttpPostedFileBase> myFiles, UploadedInformation uploadedInformation)
{
for (var i = 0; i <uploadedInformation.Documents.Length; i++)
{
uploadedInformation.Documents[i].File = myFiles[i];
}
// Do stuff
return View();
}
In the event that the order of the files cannot be assumed, you can add the filename to the data to aid with matching on the server side 如果无法假定文件的顺序,则可以将文件名添加到数据中,以帮助在服务器端进行匹配
Javascript Java脚本
var _documents = [];
for (var i = 0; i < arrayOfFiles.length; i++) {
var document = {
File: arrayOfFiles[i].file.nativeFile,
FileName: arrayOfFiles[i].file.name,
DocumentId: arrayOfFiles[i].documentId,
DocumentType: arrayOfFiles[i].documentName
};
_documents.push(document);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.