<tfoot id='CJNcs'></tfoot>
<legend id='CJNcs'><style id='CJNcs'><dir id='CJNcs'><q id='CJNcs'></q></dir></style></legend>

<small id='CJNcs'></small><noframes id='CJNcs'>

      1. <i id='CJNcs'><tr id='CJNcs'><dt id='CJNcs'><q id='CJNcs'><span id='CJNcs'><b id='CJNcs'><form id='CJNcs'><ins id='CJNcs'></ins><ul id='CJNcs'></ul><sub id='CJNcs'></sub></form><legend id='CJNcs'></legend><bdo id='CJNcs'><pre id='CJNcs'><center id='CJNcs'></center></pre></bdo></b><th id='CJNcs'></th></span></q></dt></tr></i><div id='CJNcs'><tfoot id='CJNcs'></tfoot><dl id='CJNcs'><fieldset id='CJNcs'></fieldset></dl></div>
        • <bdo id='CJNcs'></bdo><ul id='CJNcs'></ul>

      2. 向接受 ICollection&lt;IFormFile&gt; 的 ASP.NET 控制器提交多个文件

        时间:2023-07-12

            • <bdo id='5ycci'></bdo><ul id='5ycci'></ul>
            • <tfoot id='5ycci'></tfoot>

                <small id='5ycci'></small><noframes id='5ycci'>

                  <i id='5ycci'><tr id='5ycci'><dt id='5ycci'><q id='5ycci'><span id='5ycci'><b id='5ycci'><form id='5ycci'><ins id='5ycci'></ins><ul id='5ycci'></ul><sub id='5ycci'></sub></form><legend id='5ycci'></legend><bdo id='5ycci'><pre id='5ycci'><center id='5ycci'></center></pre></bdo></b><th id='5ycci'></th></span></q></dt></tr></i><div id='5ycci'><tfoot id='5ycci'></tfoot><dl id='5ycci'><fieldset id='5ycci'></fieldset></dl></div>
                • <legend id='5ycci'><style id='5ycci'><dir id='5ycci'><q id='5ycci'></q></dir></style></legend>

                    <tbody id='5ycci'></tbody>
                  本文介绍了向接受 ICollection&lt;IFormFile&gt; 的 ASP.NET 控制器提交多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在我的 ASP.NET Core 后端,我有一个如下所示的控制器函数:

                  [HttpPost][路线(文件/上传")]公共异步任务<IActionResult>UploadFile(ICollection 文件){...}

                  在我的前端,我这样调用函数:

                  var postSettings = {方法:'POST',凭据:'包括',模式:'cors'}上传文档(文件){var data = new FormData();data.append('文件', 文件);postSettings.body = 数据;return fetch(endPoint + '/documents/upload', postSettings);}

                  如果 "files" 是单个文件 - 不是包含一个文件的数组,而是单个 File 对象 - UploadFile 使用 ICollection 调用单个文件.

                  如果files"是文件列表,可以是 FileList 或 File 对象数组,则调用 UploadFile 时使用空的 ICollection.p>

                  如何提交文件列表,以便它们可以被解析为 ICollection?

                  解决方案

                  参考一次上传多个文件 - 使用 Fetch

                  uploadDocuments(endPoint, files) {var postSettings = {方法:'POST',凭据:'包括',模式:'cors'};var data = new FormData();如果(文件.长度> 0){for(var x = 0; x < files.length; x++) {//名称必须是文件",这样 .NET 才能正确绑定它data.append('文件', files.item(x));}}postSettings.body = 数据;return fetch(endPoint + '/documents/upload', postSettings);}

                  参考 使用模型绑定上传小文件

                  <块引用>

                  使用模型绑定和 IFormFile 上传文件时接口,操作方法可以接受单个 IFormFile 或一个 IEnumerable(或 List)表示几个文件.以下示例循环遍历一个或多个上传的文件,保存到本地文件系统,并返回上传文件的总数和大小.

                  [HttpPost][路线(文件/上传")]公共异步任务<IActionResult>发布(列出 文件){long size = files.Sum(f => f.Length);//临时位置文件的完整路径var filePath = Path.GetTempFileName();foreach (var formFile in files){如果(formFile.Length > 0){使用 (var stream = new FileStream(filePath, FileMode.Create)){等待 formFile.CopyToAsync(stream);}}}//处理上传的文件//不要依赖或信任未经验证的 FileName 属性.返回 Ok(new { count = files.Count, size, filePath});}

                  In my ASP.NET Core backend, I have a controller function that looks like this:

                  [HttpPost]
                  [Route("documents/upload")]
                  public async Task<IActionResult> UploadFile(ICollection<IFormFile> files)
                  {
                     ...
                  }
                  

                  In my front-end, I call the function like this:

                  var postSettings = {
                      method: 'POST',
                      credentials: 'include',
                      mode: 'cors'
                  }
                  uploadDocuments( files ) {
                      var data = new FormData();
                      data.append('files', files);   
                      postSettings.body = data;
                  
                      return fetch(endPoint + '/documents/upload', postSettings);
                  }
                  

                  If "files" is a single file - not an array with one file, but a single File object - UploadFile is called with an ICollection<IFormFile> containing the single file.

                  If "files" is a list of files, either a FileList or an array of File objects, UploadFile is called with an empty ICollection<IFormFile>.

                  How do I submit a list of files in such a way that they can be parsed as an ICollection<IFormFile>?

                  解决方案

                  Reference Uploading multiple files at once - with Fetch

                  uploadDocuments(endPoint, files) {
                      var postSettings = {
                          method: 'POST',
                          credentials: 'include',
                          mode: 'cors'
                      };
                      var data = new FormData();
                      if(files.length > 0) {
                          for(var x = 0; x < files.length; x++) {
                              // the name has to be 'files' so that .NET could properly bind it
                              data.append('files', files.item(x));    
                          }
                      } 
                      postSettings.body = data;
                  
                      return fetch(endPoint + '/documents/upload', postSettings);
                  }
                  

                  Reference Uploading small files with model binding

                  When uploading files using model binding and the IFormFile interface, the action method can accept either a single IFormFile or an IEnumerable<IFormFile> (or List<IFormFile>) representing several files. The following example loops through one or more uploaded files, saves them to the local file system, and returns the total number and size of files uploaded.

                  [HttpPost]
                  [Route("documents/upload")]
                  public async Task<IActionResult> Post(List<IFormFile> files)
                  {
                      long size = files.Sum(f => f.Length);
                  
                      // full path to file in temp location
                      var filePath = Path.GetTempFileName();
                  
                      foreach (var formFile in files)
                      {
                          if (formFile.Length > 0)
                          {
                              using (var stream = new FileStream(filePath, FileMode.Create))
                              {
                                  await formFile.CopyToAsync(stream);
                              }
                          }
                      }
                  
                      // process uploaded files
                      // Don't rely on or trust the FileName property without validation.
                  
                      return Ok(new { count = files.Count, size, filePath});
                  }
                  

                  这篇关于向接受 ICollection&lt;IFormFile&gt; 的 ASP.NET 控制器提交多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 ASP.NET Core 中使用防伪 cookie,但使用非默认 CookieName 下一篇:更新 .NET core 中身份登录页面的默认前端设计

                  相关文章

                  1. <tfoot id='mjbFq'></tfoot>

                    1. <i id='mjbFq'><tr id='mjbFq'><dt id='mjbFq'><q id='mjbFq'><span id='mjbFq'><b id='mjbFq'><form id='mjbFq'><ins id='mjbFq'></ins><ul id='mjbFq'></ul><sub id='mjbFq'></sub></form><legend id='mjbFq'></legend><bdo id='mjbFq'><pre id='mjbFq'><center id='mjbFq'></center></pre></bdo></b><th id='mjbFq'></th></span></q></dt></tr></i><div id='mjbFq'><tfoot id='mjbFq'></tfoot><dl id='mjbFq'><fieldset id='mjbFq'></fieldset></dl></div>
                        <bdo id='mjbFq'></bdo><ul id='mjbFq'></ul>
                    2. <legend id='mjbFq'><style id='mjbFq'><dir id='mjbFq'><q id='mjbFq'></q></dir></style></legend>

                      <small id='mjbFq'></small><noframes id='mjbFq'>