Api method is looks like below
[HttpPost]
public async Task> MediaBrand(IFormFile file, int brandId)
Change your method argument to take below model and add [FromForm], it should work.
public class FileUploadViewModel
{
public IFormFile File { get; set; }
public int BrandId { get; set; }
}
public async Task<BaseListResponse<MediaStorageModel>> MediaBrand([FromForm] FileUploadViewModel viewModel)
I have found a workaround to make it work:
Use
HttpPut
instead ofHttPost
on the controller action.
I was also surprised by this behavior. If someone can explain why it fixes the issue, it would help me.
In your form use
enctype="multipart/form-data"
In my case it works as follows in net core
Controller:
[HttpPost]
public IActionResult ReadFormFile([FromForm]IFormFile miCsv)
{
}
Request body: Use as key the same name as the parameter
Request Header: Use as Content-Type: multipart/form-data; boundary=xxxxxxxxxxx the boundary can be any value
If you use javascript and FormData object you need set the name of each file to 'files'
this.files.forEach((f) => {
formData.append("files", f, `${this.path}/${f.name}`);
});
if you use other name in your post you need to set it in the attribute of the post method
formData.append("someName", f, `${this.path}/${f.name}`);
public async Task<IActionResult> Post([FromForm(Name ="someName")] List<IFormFile> files)
Dont forget to set content-type
'Content-Type': 'multipart/form-data'