我正在尝试使用 createObjectURL 显示从 websql 检索到的图像。我将下面的代码放在一起来测试我是否可以从数据库中保存和检索数据。在将图像保存在数据库中之前,我可以创建 objectURL 并将其正确显示在屏幕上——然而,在从数据库中获取数据之后,什么都没有显示——或者更确切地说,显示的是默认的“图标”。我比较了从数据库中写入和检索的数据,一切似乎都是相同的。我已经为此挠头并搜索了网络,并且离答案更近了。对于我做错的任何建议/帮助,我将不胜感激。编解码器来自这里和这里。
我正在使用以下选项从命令行运行 chrome (28.0.1500.71):
google-chrome --disable-web-security --allow-file-access-from-files --unlimited-storage
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- pick any encode/decode pair - this pair seemed to work fine -->
<script src="js/base64_encode.js"></script>
<script src="js/base64_decode.js"></script>
<script>
function run(){
var gendate = new Date().getTime()
var encode = base64_encode; // replace this with whatever encode/decode pair
var decode = base64_decode; // replace this with whatever encode/decode pair
var codec = "base64_encode_decode";
var input, b64input;
var output, b64output;
var filename = "small.png" //add any file in your system
var key = gendate+filename;
console.log("Codec: "+ codec);
var db = window.openDatabase("tmpdb", "1.0", "Sql Test DB", 5 * 1024 * 1024, function(){
console.log("success")
}, function(e){
console.log(e);
});
function put(){
var req = new XMLHttpRequest();
req.open('GET', filename , false);
req.overrideMimeType('text\/plain; charset=x-user-defined');
req.onerror = function(){
console.log("ERROR: "+req.StatusText);
}
req.onload = function () {
console.log("Input file size: " +req.response.length);
var start = new Date().getTime()
input = showImage(req.response);
var data = encode(req.response);
b64input = data;
console.log("encode duration: "+ (new Date().getTime()-start));
console.log("Encoded size: " + data.length);
function _run () {
console.log("Wrote " + key);
db.transaction( function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS tmptable (key, body NONE)');
tx.executeSql('INSERT INTO tmptable (key, body) VALUES (?, ?)',[key, data]);
get();
});
}
_run();
}
req.send(null);
}
function get(){
db.transaction( function (tx) {
tx.executeSql('Select body from tmptable where key=?', [key],
function(tx, result){
var start = new Date().getTime()
b64output = result.rows.item(0).body;
var data = decode(result.rows.item(0).body)
output = data;
console.log("decode duration: "+ (new Date().getTime()-start));
console.log("Output data size: "+ data.length);
output = showImage(data);
if (b64input === b64output) {
console.log("b64 matched")
} else {
console.log("b64 not matched");
}
compareArray(input, output)
}, function(err){
console.log("ERROR: ", err)
});
});
}
function compareArray(input, output){
var match = true;
for (var i = 0; i < input; i++) {
if (input[i] !== output[i]){
console.log("Arrays not matched at: " +i);
match = false;
break;
}
}
if (match) { console.log("Arrays match")};
}
function showImage( data ) {
var byteArray = new Uint8Array(data.length);
for (var i = 0; i < data.length; i++) {
byteArray[i] = data.charCodeAt(i) & 0xff;
}
var blob = new Blob([byteArray.buffer], {type:"image/png"});
var img = document.createElement('img');
img.onload = function() {
window.URL.revokeObjectURL(blob);
};
img.src = window.URL.createObjectURL(blob);
document.body.appendChild(img);
return byteArray;
}
put();
}
</script>
<title>Insert title here</title>
</head>
<body onload="run()">
</body>
</html>
控制台输出如下:
Codec: base64_encode_decode
Input file size: 670
encode duration: 4
Encoded size: 896
Wrote 1374988636651small.png
decode duration: 1
Output data size: 670
b64 matched
Arrays match