I tried to upload a photo using IFormFile
using a postman plugin. But the API didn't get the file object from the body of the request. I tried with and without [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);
}
The error shows on this line :
var fileName = Guid.NewGuid().ToString() + Path.GetExtension(fileStream.FileName);
I figured out that it is not getting the file, while I am sending an image.jpg with the same key "fileStream"
. By the way everything else works fine. I found no solution to fix this issue. If anybody can help me with this please let me know.
fileStream
really null? Can you look at theRequest
object to see what data is being received? You should be able to see what fields the posted form actually contains. Also check with Postman’s dev tools to see what the actual request is that is being sent there. – poke