I am trying to build an api that takes an IForm file from the body and then save the file. Then return the filename in the response. I achieved the same before. But then I had an integer value as the parameter, this case it is a string. I am trying this way. But when the route matches it always returning status 200 without saving the file and filename in the response.
[HttpPost]
[Route("/api/users/${email}/photos")]
public async Task<IActionResult> Upload(string email, [FromBody]IFormFile fileStream)
{
var user = this.repository.GetUserByEmail(email);
if (user == null)
return NotFound();
var uploadsFolderPath = Path.Combine(host.WebRootPath, "Images");
if (!Directory.Exists(uploadsFolderPath))
Directory.CreateDirectory(uploadsFolderPath);
if (fileStream == null) return BadRequest("null file");
if (fileStream.Length == 0) return BadRequest("Empty file");
if (fileStream.Length >= photoSettings.MaxBytes) return BadRequest("Max file size exceeded");
if (!photoSettings.IsSupported(fileStream.FileName)) return BadRequest("Invalid file type");
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);
}
user.ImageUrl = fileName;
await unitOfWork.CompleteAsync();
return Ok(fileName);
}
Though the response is 200. it is not even showing any bad request when it should be invalid filetype.
[Route("/api/users/${email}/photos")]should be[Route("/api/users/{email}/photos")](no$) and[FromBody]should be removed.Uploadaction is being called at all - The screenshot suggests it's your home page that's being generated. You'll need to URL-encode that@too (shamimvai%40gmail.comwithout the's).