简体   繁体   English

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

[英]Sending binary data in javascript over HTTP

I'm trying to send a HTTP POST to a device on my network. 我正在尝试将HTTP POST发送到网络上的设备。 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? 反正有使用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. 这是我用来执行POST的脚本,除非我在数据字段中输入字符串,否则它目前不会运行。 Any ideas? 有任何想法吗?

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

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

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

By default, jQuery serializes the data (passed in data property) - and it means 0xFD008001 number gets 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 $.ajax property processData to 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". 默认情况下,作为对象传递给data选项的数据(从技术上讲,不是字符串)将被处理并转换为查询字符串,以适合默认的内容类型“ application / x-www-form-urlencoded” 。 If you want to send a DOMDocument, or other non-processed data, set this option to false. 如果要发送DOMDocument或其他未处理的数据,请将此选项设置为false。

... but that's only part of the whole story: XMLHttpRequest.send implementation has its own restrictions . ...但这只是整个故事的一部分: XMLHttpRequest.send实现有其自身的限制 That's why your best bet, I suppose, is to make your own serializer using TypedArrays : 我想,这就是为什么您最好的选择是使用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);

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/zh-CN/docs/Web/API/Uint32Array

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字符串。

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. 这样,您在发送fileContent时不会在控制台中收到警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM