strong textIm try to uploade a file using Asp core webApi and using IFormFile.When im try test it with postman it always show null file, even file is uploaded correctly(i think it was correct).
I try many differnt files on postman. remove the ApiController attribute, which some case suggested
Here are my code for uploade file:
[HttpPost]
public async Task<IActionResult> UploadFile([FromForm]IFormFile
fileData)
{
try
{
if (fileData == null) return BadRequest("null file");
if (fileData.Length == 0)
{
return BadRequest("empty file");
}
var folderName = Path.Combine("resources", "iles");
var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);
var fileName = ContentDispositionHeaderValue.Parse(fileData.ContentDisposition).FileName.Trim('"');
var fullpath = Path.Combine(pathToSave, fileName);
using (var stream = new FileStream(fullpath,
FileMode.Create))
{
await fileData.CopyToAsync(stream);
}
return Ok();
}
catch (Exception)
{
return StatusCode(500, "internal server error");
}
}
A few people in my team have had this same problem. When POSTing a file, you need to make sure that the name of the key
field matches up with the name of the parameter in your controller's action. In your case, that means the key
field should be fileData
: