I store some data in sessionStorage
/localStorage
. Is there any way to download that data as file attachment directly to the browser?
Asked
Active
Viewed 868 times
1

Alex
- 1,982
- 4
- 37
- 70
-
Depending on the kind of data you want to save, you could try this: https://stackoverflow.com/a/4551467/472974 – Devan Buggay Jun 17 '18 at 09:05
-
https://stackoverflow.com/questions/3749231/download-file-using-javascript-jquery – Metalik Jun 17 '18 at 09:05
-
@janaravi I have a very large string stored in `localstorage`/`sessionstorage`. I need to download that string as attachment in a form of a file. – Alex Jun 17 '18 at 09:08
1 Answers
1
Get that data from the storage and use File api to create new file in the memory
let myFile = new File(data, "Filename.txt[anything]",{type:"plain/text"}]);
let url = URL.createObjectURL(myFile);
let a = document.createElement("a");
a.href(url);
a.setAttribute("download","data.txt");
a.click();

janaravi
- 106
- 1
- 8