Zeu*_*s82 12 c# asp.net-web-api postman .net-core
我创建了一个采用任意文件的端点:
[HttpPost()]
public async Task<IActionResult> CreateFile(IFormFile file)
Run Code Online (Sandbox Code Playgroud)
当我用Postman测试时,它file总是为空.
这是我在Postman做的事情:
我究竟做错了什么?
SLD*_*Dev 10
上载一个或多个文件的完整解决方案如下所示:
此操作用于上传多个文件:
// Of course this action exist in microsoft docs and you can read it.
HttpPost("UploadMultipleFiles")]
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
return Ok(new { count = files.Count, path = filePath});
}
Run Code Online (Sandbox Code Playgroud)此操作用于上传单个文件:
[HttpPost("UploadSingleFile")]
public async Task<IActionResult> 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});
}
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
12647 次 |
| 最近记录: |