I'm trying to upload a file to an WebApi, but I'm not receiving the file. Also I don't know If the file is being appended. I'm doing this:
if (this.state.file) {
var file = this.state.file;
if (window.FormData !== undefined) {
var data = new FormData();
data.append("file", file);
$.ajax({
type: "POST",
url: "/api/service/upload",
contentType: "text/csv",
processData: false,
data: data,
success: function (result) {
console.log(result);
},
error: function (xhr, status, p3, p4) {
var err = "Error " + " " + status + " " + p3 + " " + p4;
if (xhr.responseText && xhr.responseText[0] == "{")
err = JSON.parse(xhr.responseText).Message;
console.log(err);
}
});
}
}
Also, to check the content of my Request Payload, I tried this, instead of the Ajax call:
var xhr = new XMLHttpRequest;
xhr.open('POST', '/', true);
xhr.setRequestHeader('Content-Type', 'text/csv');
xhr.send(data);
And only appears:
------WebKitFormBoundary6xZnDkSOxBAxaovA Content-Disposition: form-data; name="file"; filename="Teste.csv" Content-Type: application/vnd.ms-excel
------WebKitFormBoundary6xZnDkSOxBAxaovA--
contentType
option from ajax call. And also, please verifythis.state.file
is a file object not an array of file objects.console.log
it. – TipuZaynSultan