0

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?

1 Answer 1

3

localStorage can only contain string name/value pairs, which means you can not store an object directly.You can use stringify and parse for that.

Sign up to request clarification or add additional context in comments.

3 Comments

simply json serialize the object. :)
Guess I missed the key/value pair part of the local storage documentation. Thanks! That solves my problems.
Actually, that did solve my problem in terms of saving the object, but once I parse it, I am still unable to read it. I am guessing the file path information is removed when the object is converted to JSON. Has anyone successfully saved a file object and then read it later with the FileReader interface?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.