KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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 = "\r\n"; //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 The same file after upload shows 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(/\r\n/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)
Tags (comma-separated)
Save Edits
Cancel