I'm facing a challenge here, as I attempt to send a json object from my indexedDb along with an IFormFile object to the server at the same time. The method that handles this scenario is structured like so:
[HttpPost]
public async Task<IActionResult> Create(BatchModel model, IFormFile vinFile)
{
//logic goes here
}
Previously, this method was functioning well when I was submitting batchModels as regular JSON objects directly from the form using a POST request. However, since then, significant changes have occurred. Now, the client will upload it as soon as they reconnect online after being offline, using the following (simplified) approach:
if (infoChanged === true) {
fetchPromises.push(
fetch('/Batch/Create/', {
headers: new Headers({
'Content-Type': 'application/javascript' //Tried this with multi-part/form
}),
credentials: 'same-origin',
method: 'POST',
body: batch //, vinFile
})
);
}
return Promise.all(fetchPromises);
During testing, I attempted to utilize the method with only the model populated, and the only modification required in the C# code was adding a [FromBody] tag. However, now I need to ensure the vinFile is also filled. I experimented with using a FormData object, appending both the batch and vinFile with the same name as specified in the Create function. Unfortunately, this resulted in both variables being null.