是否可以使用javascript或phonegap以二进制模式从远程服务器读取图像?
实际上,在我的一个项目中,我需要从远程服务器读取图像,并将其作为二进制文件存储在本地数据库中,以便以后在应用程序中使用。有什么简单的方法可以做到这一点吗?这是我唯一坚持的事情,完成申请很重要。请帮忙!!提前感谢。在HTML5/ES5环境中(实际上除了Internet Explorer 9-)非常简单 首先,您需要将图像的二进制内容下载到arraybuffer中,然后将其转换为base64 datauri,这实际上是一个字符串。这可以存储在浏览器localStorage、indexedDb或webSQL中,甚至可以存储在cookie中(虽然效率不太高);稍后,您只需将这个datauri设置为要显示的图像src是否可以使用javascript或phonegap以二进制模式从远程服务器读取图像?,javascript,html,cordova,Javascript,Html,Cordova,实际上,在我的一个项目中,我需要从远程服务器读取图像,并将其作为二进制文件存储在本地数据库中,以便以后在应用程序中使用。有什么简单的方法可以做到这一点吗?这是我唯一坚持的事情,完成申请很重要。请帮忙!!提前感谢。在HTML5/ES5环境中(实际上除了Internet Explorer 9-)非常简单 首先,您需要将图像的二进制内容下载到arraybuffer中,然后将其转换为base64 datauri,这实际上是一个字符串。这可以存储在浏览器localStorage、indexedDb或web
<script>
function showImage(imgAddress) {
var img = document.createElement("img");
document.body.appendChild(img);
getImageAsBase64(imgAddress, function (base64data) { img.src = base64data; });
};
function getImageAsBase64(imgAddress, onready) {
//get from online or from whatever string store
var req = new XMLHttpRequest();
req.open("GET", imgAddress, true);
req.responseType = 'arraybuffer'; //this won't work with sync requests in FF
req.onload = function () { onready(arrayBufferToDataUri(req.response)); };
req.send(null);
};
function arrayBufferToDataUri(arrayBuffer) {
var base64 = '',
encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
bytes = new Uint8Array(arrayBuffer), byteLength = bytes.byteLength,
byteRemainder = byteLength % 3, mainLength = byteLength - byteRemainder,
a, b, c, d, chunk;
for (var i = 0; i < mainLength; i = i + 3) {
chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
a = (chunk & 16515072) >> 18; b = (chunk & 258048) >> 12;
c = (chunk & 4032) >> 6; d = chunk & 63;
base64 += encodings[a] + encodings[b] + encodings[c] + encodings[d];
}
if (byteRemainder == 1) {
chunk = bytes[mainLength];
a = (chunk & 252) >> 2;
b = (chunk & 3) << 4;
base64 += encodings[a] + encodings[b] + '==';
} else if (byteRemainder == 2) {
chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1];
a = (chunk & 16128) >> 8;
b = (chunk & 1008) >> 4;
c = (chunk & 15) << 2;
base64 += encodings[a] + encodings[b] + encodings[c] + '=';
}
return "data:image/jpeg;base64," + base64;
}
</script>
功能显示图像(imgAddress){
var img=document.createElement(“img”);
文件.正文.附件(img);
getImageAsBase64(imgAddress,函数(base64data){img.src=base64data;});
};
函数getImageAsBase64(imgAddress,onready){
//从网上或任何字符串商店获取
var req=新的XMLHttpRequest();
请求打开(“获取”,imgAddress,true);
req.responseType='arraybuffer';//这不适用于FF中的同步请求
req.onload=函数(){onready(arraybuffertodata(req.response));};
请求发送(空);
};
函数arrayBufferToDataUri(arrayBuffer){
var base64='',
编码='ABCDEFGHIJKLMNOPQRSTUVXYZABCDFGHIJKLMNOPQRSTUVXYZ0123456789+/',
bytes=新的UINT8数组(arrayBuffer),ByTeleLength=bytes.ByTeleLength,
byteRemainder=ByTeleLength%3,mainLength=ByTeleLength-byteRemainder,
a、 b,c,d,chunk;
对于(变量i=0;i>12;
c=(chunk&4032)>>6;d=chunk&63;
base64+=编码[a]+编码[b]+编码[c]+编码[d];
}
if(byteRemainder==1){
区块=字节[mainLength];
a=(区块&252)>>2;
b=(组块和3)8;
b=(chunk&1008)>>4;
c=(chunk&15)可能与@Diodeus nope重复,这是一个JavaScript问题,适用于Java。@Shailesh Thanki您能接受这个作为答案吗?有用吗?标记为答案或提供更多信息。感谢您的建议。您的代码工作得非常好。我将其打包成一个模块,用于Firefox的iMacros