2016-12-09 2 views
-1

私は過去2日間試しています。私はファイルのアップロードと2つのテキストボックスを持って、すべてが動作しており、データベースに格納されていますが、イメージはありません。テキストボックスのデータはインデックス付きのdbに格納されていますが、画像はインデックス付きのdbに格納されていません。image[]の空の配列として表示されています。これは私のjavascriptのadd関数ですどのようにインデックス付きのデータベースで画像付きテキストボックスからデータをアップロードする

function add() { 
    var a = document.getElementById("userfile"); 
    var b = a.files[0]; 

    var request = db.transaction(["todostore"], "readwrite") 
    .objectStore("todostore") 
    .add({ 
    timestamp: "KP" + (new Date()).getTime(), 
    todo: $("#todo").val(), 
    price:$("#toprice").val(), 
    image:b 
    }); 

    request.onsuccess = function(event) { 
    alert($("#todo").val() + " has been added to your database."); 
    $("#todo").val(""); 
    }; 
}; 

これはhtmlのコードです。私はエラーを特定できませんでした。

<label for="todo">To Do:</label> 
<input id="todo" type="text"> 
<input id="toprice" type="text"> 
<input type="file" id="userfile" /> 

<button onclick="add()">Add</button> 
+0

https://hacks.mozilla.org/2012/02/storing-images-and-files-in-indexeddb/ – joaumg

+0

また、http://stackoverflow.com/questions/10586006/store-an-image-file-in-indexeddb – joaumg

+0

@joaumgこの機能で何が問題なのか教えてください。 –

答えて

0

Blobを直接IndexedDBに追加することは、私にとって謎です。

我々は次のようにFileReaderを使用してBASE64などの画像を追加することができます。

function add() { 
    var files = document.getElementById("userfile").files 
    if (!files || !files.length) return 

    var fileReader = new FileReader() 
    fileReader.onload = function() { 
    var request = db.transaction(["todostore"], "readwrite") 
     .objectStore("todostore") 
     .add({ 
     timestamp: "KP" + Date.now(), 
     todo: $("#todo").val(), 
     price: $("#toprice").val(), 
     image: reader.result // changing here 
     }) 
    request.onsuccess = function(event) { 
     alert($("#todo").val() + " has been added to your database.") 
     $("#todo").val("") 
    } 
    } 
    fileReader.readAsDataURL(file[0]) 
} 
+0

todoapp.html:102 Uncaught TypeError:未定義(...)のプロパティ '0'を読み取ることができません –

+0

再確認できますか? @Endlessはそれを編集しましたが、矛盾のためにロールバックしました。あなたのコメントを見ました; \ – joaumg

関連する問題