ajax 通过 HTTP 在 JavaScript 中发送二进制数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19959072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 11:08:52  来源:igfitidea点击:

Sending binary data in javascript over HTTP

javascriptajaxbinaryhex

提问by user2984509

I'm trying to send a HTTP POST to a device on my network. I want to send four specific bytes of data to the device unfortunately I only seem to be able to send strings to the device. Is there anyway to send raw binary using javascript?

我正在尝试向网络上的设备发送 HTTP POST。不幸的是,我想向设备发送四个特定字节的数据,我似乎只能向设备发送字符串。反正有没有使用javascript发送原始二进制文件?

Here's the script I'm using to do the POST, it currently doesn't run unless I put a string in the data field. Any ideas?

这是我用来执行 POST 的脚本,它目前不会运行,除非我在数据字段中放入一个字符串。有任何想法吗?

(function ($) {
   $.ajax({
      url: '<IP of Address>',
      type: 'POST',
      contentType: 'application/octet-stream',

      //data:'253,0,128,1',
      data:0xFD008001,

      crossDomain: true
   });
})(jQuery);

回答by raina77ow

By default, jQuery serializes the data (passed in dataproperty) - and it means 0xFD008001numbergets passed to the server as '4244668417' string(10 bytes, not 4), that's why the server treats it not as expected.

默认情况下,jQuery 序列化数据(在data属性中传递) - 这意味着0xFD008001数字作为 '4244668417'字符串(10 个字节,而不是 4 个)传递给服务器,这就是服务器未按预期处理它的原因。

It's necessary to prevent such behaviour by setting $.ajaxproperty processDatato false:

有必要通过将$.ajax属性设置processData为来防止这种行为false

By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.

默认情况下,作为对象传递给 data 选项的数据(从技术上讲,除了字符串之外的任何内容)将被处理并转换为查询字符串,适合默认的内容类型“application/x-www-form-urlencoded” . 如果要发送 DOMDocument 或其他未处理的数据,请将此选项设置为 false。

... but that's only part of the whole story: XMLHttpRequest.sendimplementation has its own restrictions. That's why your best bet, I suppose, is to make your own serializer using TypedArrays:

...但这只是整个故事的一部分:XMLHttpRequest.send实施有其自身的限制。这就是为什么你最好的选择是使用TypedArrays制作你自己的序列化器:

// Since we deal with Firefox and Chrome only 
var bytesToSend = [253, 0, 128, 1],
    bytesArray = new Uint8Array(bytesToSend);

$.ajax({
   url: '%your_service_url%',
   type: 'POST',
   contentType: 'application/octet-stream',  
   data: bytesArray,
   processData: false
});

Or without using jQuery at all:

或者根本不使用 jQuery:

var bytesToSend = [253, 0, 128, 1],
    bytesArray = new Uint8Array(bytesToSend);

var xhr = new XMLHttpRequest();
xhr.open('POST', '%your_service_url%');
xhr.setRequestHeader('Content-Type', 'application/octet-stream');
xhr.send(bytesArray);

回答by Musa

You can send binary data via ajax with xhr2, you can send the data as a typed array or a blob.

您可以使用 xhr2 通过 ajax 发送二进制数据,您可以将数据作为类型化数组或blob 发送

(function ($) {
   var data = new Uint32Array(1);
   data[0] = 0xFD008001; 
   $.ajax({
      url: '<IP of Address>',
      type: 'POST',
      contentType: false,
      processData: false,
      //data:'253,0,128,1',
      data:data,

      crossDomain: true
   });
})(jQuery);

https://developer.mozilla.org/en-US/docs/Web/API/Uint32Array

https://developer.mozilla.org/en-US/docs/Web/API/Uint32Array

回答by Sohail Si

You could use atob()and btoa():

你可以使用atob()btoa()

var data = new Uint32Array(1);
data[0] = 0xFD008001;
atob(data)

This converts your binary data into a base64 string that can be sent as text.

这会将您的二进制数据转换为可以作为文本发送的 base64 字符串。

回答by Jonny

You can convert your data with type of ArrayBuffer to a ArrayBufferView like this:

您可以将类型为 ArrayBuffer 的数据转换为 ArrayBufferView,如下所示:

var fileContent = new DataView(<ArrayBuffer_data>);

With this you wont get a warning in the console when sending fileContent.

有了这个,您在发送文件内容时不会在控制台中收到警告。