2015-12-05 195 views
1

我正在开发一个使用cordova的跨平台应用程序。
我需要在sqlite中插入图像。我得到了很多Android的代码,但我发现很难做到与JavaScript。当我在iPhone中运行以下代码时,我得到了err.code:5如何在sqlite中插入图像?

document.addEventListener("deviceready", onDeviceReady, false); 
var img; 
var currentRow; 
var b = new Blob(); 
function previewFile() { 
    // var preview = document.querySelector('img'); 
    var file = document.querySelector('input[type=file]').files[0]; 
    var reader = new FileReader(); 
    // var package_name = document.getElementById("pr").value; 

    reader.onloadend = function() { 
     // img = reader.result; 
     if(file.type.match('image.*')) 
     { 
      img = reader.result; 
      // ref.push({"image":image,"service":arr,"package_name":package_name}); 
     } 
     else 
     { 
      alert("select an image file"); 
     } 
    } 

    if (file) { 
     reader.readAsDataURL(file); 
    } else { 
     preview.src = ""; 
    } 
    var image1 = encodeURI(img); 
    // var b = new Blob(); 
    b = image1; 

    console.log(b); 
    console.log(image1); 
    //document.write('<img src="'+image+'"/>'); 
} 
function onDeviceReady() { 
    var db = window.sqlitePlugin.openDatabase({name:"sqlite"}); 
    db.transaction(populateDB, errorCB, successCB); 
} 
function populateDB(tx) { 
    tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id INTEGER PRIMARY KEY AUTOINCREMENT, name,number,image BLOB)'); 
} 
function insertDB(tx) { 
    tx.executeSql('INSERT INTO DEMO (name,number,image) VALUES ("' +document.getElementById("txtName").value 
        +'","'+document.getElementById("txtNumber").value+'","' +b+ '")'); 

} 
function goInsert() { 
    var db = window.sqlitePlugin.openDatabase({name:"sqlite"}); 
    db.transaction(insertDB, errorCB, successCB); 
} 

我的html代码:

<input type="file" onchange="previewFile()">  
<button onclick="goInsert()">Insert</button> 

如何做到这一点。有人能帮我吗?在此先感谢...

+0

把它上传到服务器,并保存路径只有 – Methew

+0

谢谢@ Methew但我需要存储在sqlite的图像文件。 – Anu

+0

此线程有答案:http://stackoverflow.com/questions/11790104/how-to-storebitmap-image-and-retrieve-image-from-sqlite-database-in-android –

回答

1

将您的图像(在内存中)转换为一个字节[],然后将其保存为您的SQL数据库为varbinary(最大)。

0

将blob插入到数据库中会使您的应用程序变得缓慢且迟缓,而是将所选图片的路径保存到数据库中。

而当你想上传图像到服务器使用 fileupload cordova的功能。

如果从服务器获取图像不是在本地下载的图像和保存路径到数据库

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, 
    function onFileSystemSuccess(fileSystem) 
    { 
     fileSystem.root.getFile(
      "dummy.html", {create : true, exclusive : false}, 
      function gotFileEntry(fileEntry) 
      { 
       var sPath = fileEntry.fullPath.replace("dummy.html", ""); 
       var fileTransfer = new FileTransfer(); 
       fileEntry.remove(); 

       fileTransfer.download(
        "http://www.w3.org/2011/web-apps-ws/papers/Nitobi.pdf", 
        sPath + "theFile.pdf", 
        function(theFile) 
        { 
         console.log("download complete: " + theFile.toURI()); 
         showLink(theFile.toURI()); 
        }, 
        function(error) 
        { 
         console.log("download error source " + error.source); 
         console.log("download error target " + error.target); 
         console.log("upload error code: " + error.code); 
        } 
       ); 
      }, fail); 
    }, fail);