【发布时间】:2015-03-25 05:16:17
【问题描述】:
我正在构建一个 Chrome 应用程序,我从 XMLHTTPREQUEST 中获得了一个 .png 文件,该文件稍后使用 chrome.storage API 存储在本地,因此我可以稍后使用它,以防万一将使用该应用程序可以在没有连接到互联网的情况下访问它。问题是在 html 中你总是需要一个 url 来指定图像的存储位置,我已经尝试了一切从存储在 chrome 存储中的 .png 文件中获取一个 url,但没有任何效果。
我正在寻求一种从存储的图像中获取要在 html img 中使用的 url 的方法。
这是我获取 .png 文件并将其存储在 chrome 存储中的代码。
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://siteurl.com/image.png', true);
xhr.responseType = 'image/png';
xhr.onload = function(e) {
chrome.storage.local.set({'iconImage':this.response}, function() {
console.log('Image Saved');
});
};
xhr.send();
这是从存储中取出图像的代码。
chrome.storage.local.get(function(image){
var img = image.iconImage;
//here is where I need to create the url so I can use it to
display the image in a <img>
});
谢谢你,如果我的英语听不懂,对不起。
【问题讨论】:
-
您可以使用画布对象将图像转换为base64字符串
标签: javascript css html google-chrome-app