How to send binary data using jQuery Ajax PUT method

I am new to jQuery and I want to use jQuery Ajax to upload some files to the server, only in the PUT method. when I send a binary file (for example, gif or jpeg) to my file, the download is successful, but the contents of the binary data has been changed (it is always larger than the original file size). I am trying to change the content type or the result type of a file, but still not working. Does anyone know how to fix this?

PS: I can’t encode the contents of the binary into another form, because I can’t touch the server code.

var reader = new FileReader();

reader.onloadend = (function(Thefile) {

    $.ajax({
        url: url,
        processData:false,
        //contentType:"application/octet-stream; charset=UTF-8",
        data: file.result,
        type: 'PUT',
        success : function(){console.log("OK!");},
        error : function(){console.log("Not OK!");}
    }); 
})(file);

reader.readAsBinaryString(file);
+5
source share
1 answer

(?) UTF-8 XMLHttpRequest. , : JavaScript, 0 255.

AJAX , ArrayBuffer, File Blob.

, ArrayBuffer :

var arrBuff = new ArrayBuffer(file.result.length);
var writer = new Uint8Array(arrBuff);
for (var i = 0, len = file.result.length; i < len; i++) {
    writer[i] = file.result.charCodeAt(i);
}

arrBuff AJAX. File AJAX FileReader.

+3

All Articles