如何将文件发送到列表 - .net core web api - 邮递员

How can I send file to list - .net core web api - postman

我正在尝试将 filepostman 发送到 web apiweb api 上的方法需要一个包含 [=32] 的类型列表=]文档类型、文件和文件夹名称.. 贴在下面:

网络api方法:

[HttpPost("post-images")]
public async Task<IList<RepositoryItem>> PostAnImages (IList<PostRepoRequest> request)
{
    // rest of the code 
}

PostRepoRequest class:

public class PostRepoRequest
{
    public FileType FileType { get; set; }
    public IFormFile File { get; set; }
    public string Folder { get; set; }
}

可能会注意到我从未收到过文件,它总是空的, 我也尝试过将 header content-type 设置为 multipart/form-data 但这也没有用..

这里有什么技巧?

谢谢

干杯

有一个看不见的select框,只需将光标向左移动,您就会看到select可用区域。然后select它作为一个文件。

并在 ypur 方法参数的开头添加 [FromBody],例如 ([FromBody]IList<PostRepoRequest> request)

最后,将 Key 更新为 ...[0][file](您忘记了 [])

尝试将文件作为分隔参数发送

[HttpPost("post-images")]
public async Task<IList<RepositoryItem>> PostAnImages (IList<PostRepoRequest> request, [FromForm]List<IFormFile> files)
{
    // rest of the code 
}

在客户端(假设可以是Angular):

let input = new FormData();
for (var i = 0; i < this.filesToUpload.length; i++) {
   input.append("files", this.filesToUpload[i]);
}

您需要像这样使用点模式更改请求正文:

然后你需要给控制器输入参数添加[FromForm]属性。 还要注意邮递员和控制器中的变量名必须匹配。

[HttpPost("post-images")]
public async Task<IList<RepositoryItem>> PostAnImages ([FromForm]IList<PostRepoRequest> repositoryItems)

通过这些更改,您将能够正确获取请求: