I have the byte array in javascript. How convert this array to file for upload?
-
Is this what you are after? stackoverflow.com/questions/12603307/…Tobias Nilsson– Tobias Nilsson2013-01-09 09:12:34 +00:00Commented Jan 9, 2013 at 9:12
-
For upload? Why would you want to convert anything? Just send it via AJAX and do whatever you want with it on the server side.freakish– freakish2013-01-09 09:16:11 +00:00Commented Jan 9, 2013 at 9:16
-
Some more details about what you want will help.Pulkit Goyal– Pulkit Goyal2013-01-09 09:27:51 +00:00Commented Jan 9, 2013 at 9:27
-
There is HTML5 solution stackoverflow.com/q/23451726/2587343Vlastimil Ovčáčík– Vlastimil Ovčáčík2017-06-18 18:11:12 +00:00Commented Jun 18, 2017 at 18:11
Add a comment
|
2 Answers
you create a file like this
new File([you byte here], 'name of your file', { type: 'you extention', lastModified: new Date() });
if the your byte is string
new File([this.base64ToArrayBuffer(you string)], 'file name', { type: this.handleFileDataType(DocExtension), lastModified: new Date() });
base64ToArrayBuffer = (base64) => {
var binaryString = window.atob(base64);
var binaryLen = binaryString.length;
var bytes = new Uint8Array(binaryLen);
for (var i = 0; i < binaryLen; i++) {
var ascii = binaryString.charCodeAt(i);
bytes[i] = ascii;
}
return bytes;
}
handleFileDataType = ext => {
switch (ext) {
case 'pdf':
return 'application/pdf';
case 'jpg':
return 'image/jpeg';
case 'jpeg':
return 'image/jpeg';
case 'png':
return 'image/png';
case 'tiff':
return 'image/tiff';
case 'docx':
return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
}
};