简体   繁体   English

如何序列化Blob JavaScript

[英]how to serialize a blob javascript

I need to serialize some blobs to send to php. 我需要序列化一些Blob以发送到php。 I also want to be able to unserialize it when the php script sends it back. 我还希望能够在php脚本将其发送回时对其进行反序列化。 JSON does not stringify the contents of the blob, just metadata like the name, size, etc. How can I do this? JSON不会对Blob的内容进行字符串化处理,而只会对名称,大小等元数据进行字符串化处理。我该怎么做?

To send multiple Blob s, you can append() them to a FormData instance that you can then .send() with an XMLHttpRequest . 要发送多个Blob ,可以append()它们append()到一个FormData实例,然后可以使用XMLHttpRequest .send()

var xhr = new XMLHttpRequest();
var form = new FormData();

form.append('field-name', blob1);
form.append('field-name', blob2, 'filename.ext');

// ...

xhr.send(form);

To receive a Blob , you can set the responseType and get the response . 要接收Blob ,可以设置responseType并获取response

xhr.responseType = 'blob';
xhr.onload = function () {
    var blob = xhr.response;
};

For more info, have a look at MDN's " Sending and Receiving Binary Data ." 有关更多信息,请查看MDN的“ 发送和接收二进制数据”

Also note that all of this requires XMLHttpRequest Level 2 . 还要注意,所有这些都需要XMLHttpRequest Level 2

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

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