When I try to send data along with a file in a javascript
object to asp.net core
, the values arrive as null at the asp.net core method (List<Upload> listUpload
).
I conducted a test by removing the File property from the javascript object, and then the null issue disappeared. It seems that the problem lies with the File property not mapping correctly with the model property called File of type IFormFile.
Below is the javascript
code snippet:
jQuery('#tblDocuments > tbody > tr').each(function () {
checkBox = jQuery(this).find('td').eq(0).children();
inputFile = jQuery(this).find('td').eq(2).children()[0].files[0];
let Upload = {
File: inputFile,
CodigoVendaArquivo: res,
CodigoClienteArquivo: cliente,
Checkbox: checkBox[0].id
};
listUpload.push(Upload);
});
I'm attempting to send the data using fetch
:
fetch('../../upload', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(listUpload)
}).then(res => res.json())
.then(res => {
if (res == 1) {
// success
}
});
Now, here's my asp.net core
method:
[Route("upload")]
[HttpPost]
public JsonResult Upload([FromBody] List<Upload> listUpload)
{
// something
}
And below you can find my Model:
public class Upload
{
public IFormFile File { get; set; }
public string CodigoVendaArquivo { get; set; }
public string CodigoClienteArquivo { get; set; }
public string Checkbox { get; set; }
}