使用Postman上传ASP核心WebApi测试文件

我创建了一个采用任意文件的端点:

[HttpPost()] public async Task CreateFile(IFormFile file) 

当我使用Postman测试它时,该file始终为null。

这是我在邮差中所做的事情:

邮差截图

我究竟做错了什么?

感谢@ rmjoia的评论我得到了它的工作! 这是我在Postman中必须做的事情:

在此处输入图像描述

上传文件或文件的完整解决方案如下所示:

  • 此操作用于上载多个文件

     // Of course this action exist in microsoft docs and you can read it. HttpPost("UploadMultipleFiles")] public async Task Post(List 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 return Ok(new { count = files.Count, path = filePath}); } 

邮递员图片显示了如何将文件发送到此端点以上载多个文件: 在此处输入图像描述

  • 此操作用于上传单个文件

     [HttpPost("UploadSingleFile")] public async Task Post(IFormFile file) { // Full path to file in temp location var filePath = Path.GetTempFileName(); if (file.Length > 0) using (var stream = new FileStream(filePath, FileMode.Create)) await file.CopyToAsync(stream); // Process uploaded files return Ok(new { count = 1, path = filePath}); } 

邮递员图片显示了如何将文件发送到此端点以上传单个文件: 在此处输入图像描述