I don't seem to be able to find out why my controller receives null data. I am able to reach the controller, but no data is transmitted. Everything works fine at the controller end when I am using Postman to test the API with Body and with correct key/data content.
My controller method:
[Route("home/api/files")]
public class FileController : ControllerBase
{
[HttpPost]
public async Task<IActionResult> Post([FromForm] FileModel file)
{
if (file == null)
return BadRequest("Given data is null");
if (string.IsNullOrEmpty(file.Name))
return BadRequest("File name is undefined");
if (string.IsNullOrEmpty(file.FolderPath))
return BadRequest("FolderPath is undefined");
if (file.File.Length < 0)
return BadRequest("File content is empty");
...
}
}
The FileModel:
public class FileModel
{
public string Name { get; set; }
public string Extension { get; set; }
public string FolderPath { get; set; }
public IFormFile File { get; set; }
}
And the client-side Axios call:
export function uploadFile(folderPath, data) {
console.log("upLoadFile", folderPath, data);
const formData = new FormData();
formData.set('name', data.file);
formData.set('extension', data.extension);
formData.set('folderPath', folderPath);
formData.append('file', data);
return axios.post(
"api/files",
formData,
{ headers: { 'Content-Type': 'multipart/form-data}' }})
//{ headers: { 'Content-Type': 'multipart/form-data; boundary=${form._boundary}' }})
.then((response) => {
console.log(response.data);
console.log(response.status);
})
.catch((error) => {
console.log(error);
});
}
I assume the issue lies somewhere with the data types I am sending? Particularly, with the FileModel
s File
property type IFormFile
. All ideas are welcomed!
The used version of Axios 0.19.2