gpt4 book ai didi

JavaScript 序列化图像(图标)

转载 作者:行者123 更新时间:2023-11-30 12:51:52 27 4
gpt4 key购买 nike

我正在 CrossRider 中构建一个扩展。我需要在数据库中保存我有它们的 url 的图像/图标。它们是微小的图像,不会成为数据库中的问题。我可能有类似的东西可以访问 background.js:

<img src="http://something.com/icon.ico" alt="icon">

而且我希望能够将该图像序列化到数据库(它是一个键/值数据库)并稍后反序列化并显示它。像 HTML5 的 FileReader.readAsDataUrl() 这样的东西会很好,但我不能使用那个方法,因为它似乎太依赖表单了。

谢谢 ([-|)。

最佳答案

显示图像的 Base64 转换似乎不是必需的:

var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://g.etfv.co/http://www.google.com', true);
xhr.responseType = 'blob';
xhr.onload = function (e) {
var icon_blob = xhr.response; //That can be saved to db
var fr = new FileReader();
fr.onload = function(e) {
document.getElementById('myicon').src = fr.result; //Display saved icon
};
fr.readAsDataURL(icon_blob);
};
xhr.send(null);

这是JSFiddle .

关于JavaScript 序列化图像(图标),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20693563/

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