如何存储从下载的图像文件中获取的图像数据?

时间:2012-10-01 10:37:30

标签: javascript jquery html5

如何从下载图像文件显示图像,而不是从直接图像查看网址。

当我调用此url =“http://api.example.com/api.php?a=profilepic&auth=tokenhere&file=filename

它提供了下载文件。

通过使用$ .get函数可以获取数据,但是如何将其转换为图像格式,以便我可以存储在变量中以供进一步使用。

如果我直接使用图像标记,它可以<img src="http://api.example.com/api.php? a=profilepic&auth=tokenhere&file=filename" >,但我需要将它存储为Image对象。

即使尝试使用Image()对象也无效。

即使尝试了此http://www.maxnov.com/getimagedata/#download也无效。

无法更改服务器端api调用。

只需在仅使用js,jquery和html5脚本

的客户端进行更改

需要知道如何获取下载图像文件的base64数据

使它看起来像这样,

<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgM...."/>

期待您的重播

1 个答案:

答案 0 :(得分:0)

使用这样的代码:

var img = new Image(); 
img.onload = function () {
   // Code to execute when the image is completely downloaded.
}
img.src = "http://api.example.com/api.php? a=profilepic&auth=tokenhere&file=filename";