gpt4 book ai didi

javascript - 来自 DataURL 的 Blob?

转载 作者:IT王子 更新时间:2023-10-29 02:51:29 26 4
gpt4 key购买 nike

使用 FileReaderreadAsDataURL() 我可以将任意数据转换为数据 URL。有没有办法使用内置浏览器 api 将数据 URL 转换回 Blob 实例?

最佳答案

用户 Matt 在一年前 (How to convert dataURL to file object in javascript?) 提出了以下代码,这可能对您有帮助

编辑:正如一些评论者所报告的,BlobBuilder 已在一段时间前被弃用。这是更新后的代码:

function dataURItoBlob(dataURI) {
// convert base64 to raw binary data held in a string
// doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
var byteString = atob(dataURI.split(',')[1]);

// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]

// write the bytes of the string to an ArrayBuffer
var ab = new ArrayBuffer(byteString.length);

// create a view into the buffer
var ia = new Uint8Array(ab);

// set the bytes of the buffer to the correct values
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}

// write the ArrayBuffer to a blob, and you're done
var blob = new Blob([ab], {type: mimeString});
return blob;

}

关于javascript - 来自 DataURL 的 Blob?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12168909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com