Sorry about my english. I need upload my file (.exe) selected by input file:
<input type="file" id="myfile">
Read like this(Javascript):
var myfile='';
var input = document.getElementById('myfile');
input.onchange = function(evt){
var tgt = evt.target || window.event.srcElement, files = tgt.files;
if (FileReader && files && files.length) {
var fr = new FileReader();
fr.onload = function(){
myfile = fr.result;
}
fr.readAsDataURL(files[0]);
}
}
Now I have variable "myfile" like:
"data:application/msdownload;base64,0J/RgNC40LLQtdGCINC80LjRgCE= .... etc."
Inside base64 part I have source file what I was select. When I trying upload my file, encoding and size of this file changed and file is corrupted. What am doing wrong?
Upload code:
var fd = new FormData();
var b = new Blob([atob(decodeURIComponent((myfile).split(',')[1]))],{type: 'application/msdownload'});
fd.append('file', b, "myfile.exe");
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://myserver/");
xhr.send(fd);
File upload OK. BUT when I download this file, this file corrupted...encoding and size changed.
I was trying set different headers like this:
xhr.setRequestHeader("Content-Type", "charset=windows-1251");
.............
xhr.setRequestHeader("Content-Type", "charset=utf-8");
etc...... but nothing changed...
I can upload my file without ajax, but I need hold this file localy ...and after manipulation upload it from variable.
In short:
I have a string encoded in Base64 like this:
0J/RgNC40LLQtdGCINC80LjRgCE=
Well, I know at this string is source of file "SecretFile.exe". I want decode and upload this file using javascript. With standart window.atob decoded string is not equal of original file source. How true decode this file, encoded by Base64 in FileReader.
Thank you.