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?
我是JQuery的新手,我想使用JQuery Ajax将一些文件上传到服务器,仅在PUT方法中。当我向我的服务器发送一些二进制文件(例如gif或jpeg)时,上传成功,但二进制数据的内容发生了变化(它总是大于原始文件大小)。我尝试更改内容类型或文件结果类型,但仍然无法正常工作。谁知道如何解决这个问题?
PS: I cannot encode the binary file's content into other form, because I cannot touch the code of server.
PS:我无法将二进制文件的内容编码成其他形式,因为我无法触及服务器的代码。
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);
1 个解决方案
#1
3
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.
大多数(全部?)浏览器在通过XMLHttpRequest发送时会自动将字符串数据类型转换为UTF-8。当您将文件作为二进制字符串读取时,您将获得以下内容:JavaScript字符串对象,其中每个字符的值介于0到255之间。
To force the AJAX request to send binary encoded data, the data parameter must be an ArrayBuffer
, File
, or Blob
object.
要强制AJAX请求发送二进制编码数据,data参数必须是ArrayBuffer,File或Blob对象。
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:
如果在发送之前需要访问文件的内容,可以使用以下代码将二进制字符串转换为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);
}
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.
然后你将arrBuff实例传递给AJAX函数。否则,您应该能够将File对象本身传递给AJAX函数,而无需使用所有FileReader代码。
#1
3
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.
大多数(全部?)浏览器在通过XMLHttpRequest发送时会自动将字符串数据类型转换为UTF-8。当您将文件作为二进制字符串读取时,您将获得以下内容:JavaScript字符串对象,其中每个字符的值介于0到255之间。
To force the AJAX request to send binary encoded data, the data parameter must be an ArrayBuffer
, File
, or Blob
object.
要强制AJAX请求发送二进制编码数据,data参数必须是ArrayBuffer,File或Blob对象。
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:
如果在发送之前需要访问文件的内容,可以使用以下代码将二进制字符串转换为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);
}
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.
然后你将arrBuff实例传递给AJAX函数。否则,您应该能够将File对象本身传递给AJAX函数,而无需使用所有FileReader代码。