简体   繁体   中英

Save Javascript file object to local storage

I have a input type file where I save the file to a file object like so

var uploadControl = document.getElementById("fileUpload");
var files = uploadControl.files[0];

Then I would like to save that file to local storage to read into the FileReader object at a later time. Does anyone know if this is possible. I have tried several different options and every time I try and retrieve the object out of local storage, it is undefined.

Here are some things I have tried

var setObj = {"prop": "myProp", "file": files};
chrome.storage.sync.set({"myObj":setObj});

This doesn't throw errors, but when I try and retrieve the object, it is undefined

chrome.storage.sync.get("myObj", function(item) {
console.log("item name: " + item.myObj.file.name);
});

However I can access the other properties of the object

chrome.storage.sync.get("myObj", function(item) {
    console.log("item prop: " + item.myObj.prop);
    });

Am I doing something wrong when adding the object to local storage or am I accessing it incorrectly? Or is it just impossible to do this?

localStorage只能包含字符串名称/值对,这意味着您不能直接存储对象。您可以为此使用stringifyparse

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM