Reputation: 418
The below code is my UploadImagea
action in my controller.
[HttpPost]
[ActionName("UploadImage")]
[AllowAnonymous]
[Route("[action]")]
public async Task<string> UploadImage( IFormFile file, string folder, int SchoolId, byte EnvironmentType)
{
_galleryService.InvokeAzureSettings(SchoolId, EnvironmentType);
try
{
var res = await ((GalleryService)_galleryService).UploadImage(folder, file);
return res;
}
catch (Exception ex)
{
return ex.ToString();
}
}
The below screenshots is from my postman, the Content-Type I'm testing is multipart/form-data
. All other actions are working fine. I don't know why I get 415 error code, how to solve it?
Upvotes: 1
Views: 1694
Reputation: 361
No need to define any file in body just check if the client side is sending file in body by using HttpRequest from IHttpContextAccessor.
[HttpPost]
public IActionResult UploadDocument()
{
var httpRequest = context.HttpContext.Request;
List<IFormFile> lstFiles = new List<IFormFile>();
foreach (var files in httpRequest.Form.Files)
{
lstFiles.Add(files);
}
return Ok();
}
Upvotes: 1
Reputation: 247098
Create a model to hold the desired form data
public class Model {
public IFormFile file { get; set; }
public string folder { get; set; }
public int SchoolId { get; set; }
public byte EnvironmentType { get; set; }
}
Update the action to expect the data from a form using the appropriate attribute
[HttpPost]
[ActionName("UploadImage")]
[AllowAnonymous]
[Route("[action]")]
public async Task<string> UploadImage([FromForm] Model model) {
_galleryService.InvokeAzureSettings(model.SchoolId, model.EnvironmentType);
try {
var res = await ((GalleryService)_galleryService).UploadImage(model.folder, model.file);
return res;
} catch (Exception ex) {
return ex.ToString();
}
}
Upvotes: 3