如何通过JQuery Ajax的PUT方法来发送二进制数据 [英] How to send binary data via JQuery Ajax PUT method

查看:1120
本文介绍了如何通过JQuery Ajax的PUT方法来发送二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的jQuery和 我想使用jQuery Ajax的一些文件上传到服务器,只有在PUT方法。 当我发送一些二进制文件(如GIF或JPEG)到我的服务器,上传成功,但二进制数据的内容被改变(它总是比原始文件大小越大)。 我试图改变内容类型或文件结果的类型,但还是不行。 任何人都知道如何解决这一问题?

PS:我不能连接code中的二进制文件的内容转换成其他形式的,因为我不能触摸服务器的code

  VAR读卡器=新的FileReader();

reader.onloadend =(函数(Thefile){

    $阿贾克斯({
        网址:网址,
        过程数据:假的,
        //的contentType:应用程序/八位字节流,字符集= UTF-8,
        数据:file.result,
        键入:把',
        成功:函数(){执行console.log(OK!);},
        错误:函数(){执行console.log(!不正常);}
    });
})(文件);

reader.readAsBinaryString(文件);
 

解决方案

大多数(所有?)浏览器会自动转换成字符串数据类型转换为UTF-8通过向他们发送当 XMLHtt prequest 。当你阅读的文件作为二进制字符串,你完全,鉴于:一个JavaScript字符串对象,其中每一个字符是0和255之间的值

要强制AJAX请求发送二进制EN codeD数据,数据参数必须是一个 ArrayBuffer 文件的Blob 对象。

如果您需要在发送前访问该文件的内容,则可以将二进制字符串转换到 ArrayBuffer 具有以下code:

  VAR arrBuff =新ArrayBuffer(file.result.length);
VAR作家=新Uint8Array(arrBuff);
对于(VAR I = 0,len个= file.result.length; I< LEN;我++){
    作家[我] = file.result.char $ C $猫(我);
}
 

和你通 arrBuff 实例的AJAX功能。否则,你应该能够在文件对象本身传递到了AJAX功能,无需所有的的FileReader $ C $的℃。

I am new to JQuery and I want to use JQuery Ajax to upload some files to server, Only in PUT method. when I send some binary file(such as gif or jpeg) to my server, upload succeed, but the content of binary data was changed(it's always bigger than original file size). I try to change the content-type or the type of file result, but still not work. Anyone knows how to fix this?

PS: I cannot encode the binary file's content into other form, because I cannot touch the code of server.

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);

解决方案

Most (all?) browsers will automatically convert string datatypes to UTF-8 when sending them via an XMLHttpRequest. When you read the file as a binary string, you're given exactly that: a JavaScript string object where each character is a value between 0 and 255.

To force the AJAX request to send binary encoded data, the data parameter must be an ArrayBuffer, File, or Blob object.

If you need access to the contents of the file before sending, you can convert the binary string to an ArrayBuffer with the following code:

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);
}

And you'd pass the arrBuff instance to the AJAX function. Otherwise, you should be able to pass the File object itself into the AJAX function without all of your FileReader code.

这篇关于如何通过JQuery Ajax的PUT方法来发送二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆