I am trying to invoke a jquery ajax POST request which has formdata as multiple files located on file server. This is my ajax request;
JavaScript
x
18
18
1
var formData = new FormData();
2
$.ajax({
3
url: 'url',
4
method: 'POST',
5
contentType: false,
6
processData: false,
7
data: formData,
8
beforeSend: function (xhr) {
9
xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password));
10
},
11
success: function(res){
12
console.log('successfully')
13
},
14
error: function(){
15
console.log('error')
16
}
17
})
18
where formData has files located somewhere on the file server which I tried to fetch using jquery get and tried to add in formData on document ready like below;
JavaScript
1
6
1
$.get("http://localhost/file.xml", function(data) {
2
var data1 = data;
3
var file = new File([data1], "file.xml");
4
formData.append('file', file);
5
});
6
Is it possible to pass or create formData having files located on the file server in ajax POST request?
Advertisement
Answer
You would need to convert it to a blob
JavaScript
1
9
1
$.ajax({
2
url: "http://localhost/file.xml",
3
method: "GET",
4
dataType: "text"
5
}).done(function( content ) {
6
var blob = new Blob([content], { type: "text/xml" });
7
formData.append("file", blob);
8
});
9