我必须将 Blob 作为字符串发送并将其转换回 Blob。方法 blob.text() 返回一个 promise ,其内容为字符串。但是如何将此字符串转换回 blob?我想将其转换为图像数据 url。
https://developer.mozilla.org/en-US/docs/Web/API/Blob
最佳答案
要将字符串转换为 blob,请使用 new Blob
界面:
const blob = new Blob([string], {
type: 'image/jpeg' // or whatever your Content-Type is
});
见 this section of the document you linked to .如果您有
Blob
对象调用 blob
, blob.type
将给出其内容类型。因此,您可以按如下方式解构和重建它:const string = await blob.text();
const type = blob.type;
const blob2 = new Blob([string], {type: type});
关于Javascript 将 Blob 对象转换为字符串并返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68276368/