好吧,我正在通過xhr下載數據塊,並且我希望將其存儲在本地以用於擴展chrome。將二進制數據作爲字符串存儲在Web Sql數據庫中
起初我能夠將它存儲在localStorage
中,但後來我需要移動到localStorage無法處理的較大的塊。因此我決定將它存儲在Web Sql Database中。但是,我能夠將數據完全保存在localStorage
中,但是當我嘗試將其存儲在數據庫中時,數據丟失或出現錯誤。
像這樣:
//this way the data gets lost because it is filtered to prevent SQL injection attacks
tx.executeSql('INSERT INTO files (id, content) VALUES (?,?)', [fileID,file]);
//this method gives an error (unrecognized token) because the data contains byte values in the string
tx.executeSql('INSERT INTO files (id, content) VALUES (?, "' + file + '")', [id]);
我能想到得到的數據到數據庫的唯一另一種方式是通過使用btoa(file)
,和它的作品,但我認爲業績將相當可觀命中每次數據存儲/使用時必須執行btoa
/atob
。
所以我的問題是,如何在不使用btoa
的情況下將數據存儲在數據庫中,或者如果btoa
使用多少性能不可能?
我也很確定localStorage
使用SQLite以及Web Sql數據庫,所以爲什麼我不能像localStorage
那樣簡單地將數據放在後者中?