简体   繁体   English

FormData密钥不通过

[英]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.

相关问题 使用RsaProtectedConfigurationProvider的ProtectSection Key在哪里? - ProtectSection with RsaProtectedConfigurationProvider where does the Key go? 更新 function 不会通过其中的所有代码行更新 go - Unity - update function does not go through all the line of codes inside it - Unity 无法通过 FormData Append 传递 HTML 内容 - Unable to pass HTML contents through FormData Append 如何通过FormData将视频文件上传到Azure Datalake? - How to upload video file through FormData to Azure Datalake? 为什么我的播放器仅在前进时会穿过物体? - Why does my player go through objects only when moving forward? 在迭代数组时增加指向数组的指针时,不安全代码是否越界? - Does unsafe code go out of bounds when incrementing a pointer to an array while iterating through the array? UDP套接字是否需要像TCP套接字一样通过Accept Process? - Does UDP socket need to go through the Accept Process like TCP sockets? do-while 循环不会 go 通过 while 条件 C# - do-while loop does not go through while condition C# 如果没有通过TMG获取错误 - If not go through TMG get error 使用无法访问私钥的客户端证书通过HttpClient进行HTTPS调用 - Make HTTPS call through HttpClient with client certificate that does not have access to private key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM