我尝试IFormFile
使用邮递员插件上传照片。但是 API 没有从请求正文中获取文件对象。我试过有和没有[FromBody]
.
[HttpPost]
public async Task<IActionResult> Upload(int vId, IFormFile fileStream)
{
var vehicle = await this.repository.GetVehicle(vId, hasAdditional: false);
if (vehicle == null)
return NotFound();
var uploadsFolderPath = Path.Combine(host.WebRootPath, "uploads");
if (!Directory.Exists(uploadsFolderPath))
Directory.CreateDirectory(uploadsFolderPath);
var fileName = Guid.NewGuid().ToString() + Path.GetExtension(fileStream.FileName);
var filePath = Path.Combine(uploadsFolderPath, fileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await fileStream.CopyToAsync(stream);
}
错误显示在这一行:
var fileName = Guid.NewGuid().ToString() + Path.GetExtension(fileStream.FileName);
我发现它没有获取文件,而我正在发送具有相同 key 的 image.jpg "fileStream"
。顺便说一句,其他一切都很好。我没有找到解决此问题的解决方案。如果有人可以帮助我,请告诉我。