Search code examples
javascriptgoogle-chromegoogle-chrome-extensiongoogle-chrome-appgoogle-chrome-storage

Can we store files in Chrome Storage, instead of strings


I could read in the Chrome storage API page that we can store string values inside Chrome storage against keys. Is there any way to store files against keys inside Chrome storage?


Solution

  • chrome.storage stores data as the JSON stringification of the values which it's given to store. So, yes, if you convert your file(s) into a form that can be converted to JSON (e.g. by JSON.stringify()), then the contents of the file can be stored. If the value that you are trying to store in chrome.storage can not be converted to JSON, then it can't be stored (e.g. DOM elements). chrome.storage does not inherently care what the data represents, only that it can be JSON stringified.

    If you're asking, as stated in your comment, if it's a good idea to store thousands of different "files" totaling more than 5GB in chrome.storage, then the answer is: "NO!".

    If you are looking for alternatives, then some are provided in Can you use HTML5 local storage to store a file? If not, how?