Я использую внешний класс для преобразования переменной объекта в переменную FormData.Проходят все ключи, все кроме одного для объекта File.
Вот этот внешний класс: https://github.com/therealparmesh/object-to-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
});
Код для контроллера:
[HttpPost]
[ActionName("UploadFile")]
public ActionResult UploadFile(UploadedInformation uploadedInformation)
{
_ = Request.Form;
return View();
}
Класс загруженного файла:
public class UploadedInformation
{
public long LoanID { get; set; }
public IEnumerable<Document> Documents { get; set; }
}
Класс документа:
public class Document
{
public HttpPostedFileBase File { get; set;}
public string DocumentId { get; set;}
public string DocumentType { get; set; }
}
Все элементы связываются отлично, кромедля File
.В браузере ключи и значения отладчика:
LoanID, 1452465
documents[0][File], [object File]
documents[0][DocumentId], 1
documents[0][DocumentType], Passport
_=Request.Form
также отображает только 3 клавиши без documents[0][File]
Обновление: Я изменил контроллер на
public ActionResult UploadFile(IEnumerable<HttpPostedFileBase> file, IEnumerable<string> documentType, IEnumerable<string>documentId, long loanId){...}
и _=Request.Form
по-прежнему ничего не показывает с file
, однако список файлов заполнен
Другое обновление: По-видимому, на этот раз элементы файла отображаются только в _=Request.File