I have read How do I upload a file with the JS fetch API?
and
Getting “TypeError: Failed to fetch” when the request hasn’t actually failed
I have some javascript that sends a javascript object to WebApi backend, and it works!
I am now trying to use the same code to upload a file.
This is what I’m trying to upload to (my C# WebApi 2)
public async Task UploadImage([FromBody] object o)
{
//do something
}
and the javascript
const file = inputTypeFile.files[0];
async function withData(url = '', data = {}, verb, success) {
const response = await fetch(url, {
method: verb,
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: getHeader(),
referrerPolicy: 'no-referrer',
body: file
}).catch(function (e) {
errorHandler(e);
});
I always get a failure
Failed to fetch at withData
If I change the value of file
to a valid json object, like {some:123}
then it works fine.