Reputation: 13
I am using FileReader() to get the file values. How can I make Http post request to post multiple file values as input.
var fi = document.getElementById('myFile');
if (fi.files.length > 0) {
for (var i = 0; i < fi.files.length; i++) {
var reader = new FileReader();
reader.readAsDataURL(fi.files[i]);
var f = fi.files[i];
file_input = reader.result.split("base64,").pop();
var attachment_type = fi.files[i].type;
var filename = fi.files[i].name;
}
}
HTTP request
var data = JSON.stringify(
{
"firstname": First,
"lastname": Last,
"file": [
{
"input_file": file_input,
"attachment_type": attachment_type,
"file_name": filename
} ] } );
var url = "";
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true)
xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
xhr.send(data);
Upvotes: 0
Views: 150
Reputation: 166
You can pass the arrays of input_file, attachemnt_type, file_name and process the files using array indices.
Upvotes: 1