2015-12-05 136 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);