简体   繁体   English

使用 ASP.net 内核 web api 发送多个文件

[英]Send multiple files using ASP.net core web api

I need to send multiple files to ASP.net core webApi method.I have tried as shown below.But it always shows as 0 files .Can you tell me why?我需要将多个文件发送到 ASP.net 核心 webApi 方法。我试过如下所示。但它总是显示为0 files 。你能告诉我为什么吗?

[Route("api/[controller]/[action]")]
[Consumes("application/json", "application/json-patch+json", "multipart/form-data")]
public class DocumentUploadController : CpcpControllerBase
{
    [HttpPost]
    public async Task<List<string>> AddDocument(ICollection<IFormFile> files)
    {
        foreach (var f in files)
        {
            var stream = f.OpenReadStream();
            var name = f.FileName;
        }
    }
}

Postman: Postman:

在此处输入图像描述

But I can send 1 file as shown below.It's working fine.但我可以发送 1 个文件,如下所示。它工作正常。

[HttpPost]
public async Task<string> AddDocument(IFormFile file)
{
        var stream = file.OpenReadStream();
        var name = file.FileName;           
}

Replace key file1 by files in postman. 用邮递员中的files替换密钥file1 It works for me. 这个对我有用。

每个文档使用IFormFileCollection而不是ICollection<IFormFile>

Replace key file1 by files in postman.将密钥 file1 替换为 postman 中的文件。 Also we can also access files from Http Context as:此外,我们还可以从 Http 上下文访问文件,如下所示:

var files = HttpContext.Request.Form.Files;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM