我需要序列化一些 blob 以发送到 php。我还希望能够在 php 脚本将其发回时对其进行反序列化。 JSON 不会对 blob 的内容进行字符串化,而只会对名称、大小等元数据进行字符串化。我该如何执行此操作?
最佳答案
要发送多个 Blob
,您可以将它们 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);
要接收Blob
,您可以set the responseType
and get the response
.
xhr.responseType = 'blob';
xhr.onload = function () {
var blob = xhr.response;
};
有关更多信息,请查看 MDN 的“Sending and Receiving Binary Data”。
另请注意,所有这些都需要 XMLHttpRequest Level 2 .
关于javascript - 如何序列化 blob javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18394483/