Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
62
rated 0 times [  63] [ 1]  / answers: 1 / hits: 151099  / 13 Years ago, thu, september 15, 2011, 12:00:00

Trying to use fileReader.readAsBinaryString to upload a PNG file to the server via AJAX, stripped down code (fileObject is the object containing info on my file);



var fileReader = new FileReader();

fileReader.onload = function(e) {
var xmlHttpRequest = new XMLHttpRequest();
//Some AJAX-y stuff - callbacks, handlers etc.
xmlHttpRequest.open(POST, '/pushfile', true);
var dashes = '--';
var boundary = 'aperturephotoupload';
var crlf = rn;

//Post with the correct MIME type (If the OS can identify one)
if ( fileObject.type == '' ){
filetype = 'application/octet-stream';
} else {
filetype = fileObject.type;
}

//Build a HTTP request to post the file
var data = dashes + boundary + crlf + Content-Disposition: form-data; + name=file; + filename= + unescape(encodeURIComponent(fileObject.name)) + + crlf + Content-Type: + filetype + crlf + crlf + e.target.result + crlf + dashes + boundary + dashes;

xmlHttpRequest.setRequestHeader(Content-Type, multipart/form-data;boundary= + boundary);

//Send the binary data
xmlHttpRequest.send(data);
}

fileReader.readAsBinaryString(fileObject);


Examining the first few lines of a file before upload (using VI) gives me



enter



The same file after upload shows



enter



So it looks like a formatting/encoding issue somewhere, I tried using a simple UTF8 encode function on the raw binary data



    function utf8encode(string) {
string = string.replace(/rn/g,n);
var utftext = ;

for (var n = 0; n < string.length; n++) {

var c = string.charCodeAt(n);

if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}

}

return utftext;
)


Then in the original code



//Build a HTTP request to post the file
var data = dashes + boundary + crlf + Content-Disposition: form-data; + name=file; + filename= + unescape(encodeURIComponent(file.file.name)) + + crlf + Content-Type: + filetype + crlf + crlf + utf8encode(e.target.result) + crlf + dashes + boundary + dashes;


which gives me the output of



enter



Still not what the raw file was =(



How do I encode/load/process the file to avoid the encoding issues, so the file being received in the HTTP request is the same as the file before it was uploaded.



Some other possibly useful information, if instead of using fileReader.readAsBinaryString() I use fileObject.getAsBinary() to get the binary data, it works fine. But getAsBinary only works in Firefox. I've been testing this in Firefox and Chrome, both on Mac, getting the same result in both. The backend uploads are being handled by the NGINX Upload Module, again running on Mac. The server and client are on the same machine. The same thing is happening with any file I try to upload, I just chose PNG because it was the most obvious example.


More From » encoding

 Answers
9

Use fileReader.readAsDataURL( fileObject ), this will encode it to base64, which you can safely upload to your server.


[#90090] Tuesday, September 13, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alora

Total Points: 284
Total Questions: 99
Total Answers: 92

Location: Singapore
Member since Sat, Jul 25, 2020
4 Years ago
;