Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save data from localStorage to csv

Is it possible to save the data from local storage to a csv file?

First i want to fill in a html form, after that Some pictures are shown with rating buttons. My idea is to store all the input in the local storage and then save all to a csv file (this should be saved on a server) Is there any way to save all the data at the end to a csv file?

like image 571
user2355759 Avatar asked Sep 13 '25 04:09

user2355759


1 Answers

Using Blob (https://developer.mozilla.org/en/docs/DOM/Blob) this seems achievable. First generate the file from the local store then blast it off to the server however you like. This should get you going in the right direction:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CSV Export</title>
<script>
    function exportData() {
        var item = localStorage.csv=",what you want in the CSV,";

        var ary = localStorage.getItem( "csv" ); //csv as a string
        var blob = new Blob([ary], {type: "text/csv"});
        var url = URL.createObjectURL(blob);
        var a = document.querySelector("#results"); // id of the <a> element to render the download link
        a.href = url;
        a.download = "file.csv";

    }
</script>
</head>
<body>
    <button onclick="exportData()">Download CSV</button><br>
    <a id="results">CSV from local store</a>
    </body>
</html>

Getting the file onto the server is another matter but you should be able to tweak this and use PHP, .NET or whatever else.

like image 75
jusynth Avatar answered Sep 14 '25 19:09

jusynth