How to send binary data via JQuery Ajax PUT method -
I'm new to jQuery and I want to use JQuery Ajax to upload some files to the server, only When I send some binary files (such as gif or jpeg) to my server in a seam method, the upload is successful, but the content of the binary data was changed (it is always larger than the original file size). I try to change the content type or file result type, but still does not work, how does this fix any?
PS: I can not encode the contents of the binary file in another form, because I can not touch the server's code.
var reader = new FileReader (); Reader.loadload = (function (dfile) {$ .ajax ({url: url, process data: false, // content type: "app / octet-stream; charset = uTF-8", data: file.result, type : 'PUT', success: function () {console.log ("okay!");}, Error: function () {console.log ("not ok!");}});}) ); Reader.readAsBinaryString (file);
Most (all?) Browsers automatically send string datatypes to UTF-8 Send them through a
XMLHttpRequest
When you read the file as a binary string, you are given exactly this: a JavaScript string object where each character has a value between 0 and 255.
Must be an object.
If you need access to the content file before sending, you can convert it to a
ArrayBuffer
with the following code:var arrBuff = new ArrayBuffer (file.result.length); Var Author = New Uint8Array (arrBuff); For (var i = 0, len = file.result.length; i & lt; len; i ++) {author [i] = file.result.charCodeAt (i); }
and you want to pass
arrBuff
to the AJAX function. Otherwise, you should have yourFileReader
code