最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Save data from localStorage to csv - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked May 7, 2013 at 21:26 user2355759user2355759 431 silver badge3 bronze badges 2
  • "this should be saved on a server" - Which server-side language(s) are you using? You can certainly submit the data from the browser to the server (possibly with Ajax), but how you'd make the server save a CSV would depend on the server technology/language. – nnnnnn Commented May 7, 2013 at 21:29
  • I thougt of using php to save it on the server – user2355759 Commented May 7, 2013 at 21:31
Add a ment  | 

1 Answer 1

Reset to default 5

Using Blob (https://developer.mozilla/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.

发布评论

评论列表(0)

  1. 暂无评论