简体   繁体   English

通过JavaScript XHR2对象在PHP中发送ArrayBuffer

[英]Sending an ArrayBuffer in PHP through JavaScript XHR2 object

Here it is, I'm writing the new version of an AJAX library based on promises with XHR2 support ( https://github.com/pyrsmk/qwest ). 在这里,我正在编写基于XHR2支持的promises的AJAX库的新版本( https://github.com/pyrsmk/qwest )。 I'm currently writing the unit tests and 95% of the library is now robust. 我目前正在编写单元测试,现在该库的95%都很强大。 Anyway, I need to verify that we can successfully retrieve an ArrayBuffer (XHR2 supports 'arraybuffer' response type) but how can I generate this ArrayBuffer with PHP? 无论如何,我需要验证我们是否可以成功检索ArrayBuffer(XHR2支持'arraybuffer'响应类型),但是如何使用PHP生成此ArrayBuffer? What protocol do I use? 我使用什么协议? Base64? Base64编码?

I really have no idea how I should handle the whole thing... 我真的不知道该如何处理整个事情...

EDIT : it seems that when I send an ArrayBuffer to PHP, the data is handled directly by the $_POST variable 编辑:似乎当我将ArrayBuffer发送到PHP时,数据直接由$ _POST变量处理

Intro 介绍

Well, you don't send an "ArrayBuffer". 好吧,您不会发送“ ArrayBuffer”。 What you send is data and specify a format to make sense of that data. 您发送的是数据,并指定一种使该数据有意义的格式。 The client, then, can choose the best way to interpret it. 然后,客户可以选择最佳的解释方式。

So, an ArrayBuffer is just a generic fixed-length container for data (binary data) that enables you to create "views" of the underlying data using JavaScript typed arrays. 因此, ArrayBuffer只是数据(二进制数据)的通用定长容器 ,使您能够使用JavaScript类型的数组创建基础数据的“视图”。 The cool thing is that multiple views can be created from a single ArrayBuffer source. 很棒的事情是可以从单个ArrayBuffer源创建多个视图。


That being said, in your specific case, you can send binary data in PHP the same way you send any data in PHP. 话虽这么说,在您的特定情况下,您可以使用PHP发送二进制数据的方式与使用PHP发送任何数据的方式相同。

Example: 例:

server.php server.php

$filename = 'img.png';
$fsize = filesize($filename);

$handle = fopen($filename, "rb");
$contents = fread($handle, $fsize);
fclose($handle);

header('content-type: image/png');
header('Content-Length: ' . $fsize);

echo $contents;

or shorter 或更短

$filename = 'img.png';

header('content-type: image/png');
header('Content-Length: ' . filesize($filename));

readfile($filename);

client.js client.js

var xhr = new XMLHttpRequest();

xhr.open('GET', 'server.php', true);
xhr.responseType = 'arraybuffer';

xhr.onload = function(e) {
    var uInt8Array = new Uint8Array(this.response);
    console.log(uInt8Array);
};

xhr.send();

Handling binary data in php is a little bit tricky. 在php中处理二进制数据有些棘手。 To response an ArrayBuffer you must cast it to a binary string and result it with an echo statement. 要响应ArrayBuffer,必须将其转换为二进制字符串,并使用echo语句生成结果。

To generate a binary string from an 8 bit array, it is necessary to use the pack statement. 要从8位数组生成二进制字符串,必须使用pack语句。 If the server does not support PHP version 5.6+, each array item can be converted and concat with the dot operator: 如果服务器不支持PHP 5.6+版本,则可以转换每个数组项,并使用点运算符进行连接:

server.php server.php

<?php
    header('content-type: application/octet-stream');

    $arrayBuffer = array(0, 1, 2, 253, 254, 255);

    $binary = "";
    for($index = 0; $index < count($arrayBuffer); $index++)
    {
        $binary = $binary.pack("C*", $arrayBuffer[$index]);
    }

    echo $binary;
?>

The MIME type of raw data is application/octet-stream . 原始数据的MIME类型是application / octet-stream At client side of the JavaScript code looks like: JavaScript代码的客户端看起来像:

client.js client.js

let xmlRequest = new XMLHttpRequest();
xmlRequest.open('POST', 'server.php');
xmlRequest.responseType = 'arraybuffer';
xmlRequest.onload = event =>
{
    let arrayBuffer = new Uint8Array(event.target.response);
    console.log('arrayBuffer: ', arrayBuffer); // [0, 1, 2, 253, 254, 255];
};
xmlRequest.send();

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

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