简体   繁体   English

将数据从localStorage保存到csv

[英]Save data from localStorage to csv

Is it possible to save the data from local storage to a csv file? 是否可以将数据从本地存储保存到csv文件?

First i want to fill in a html form, after that Some pictures are shown with rating buttons. 首先,我想填写一个html表单,之后,某些图片会显示带有评级按钮。 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? 我的想法是将所有输入存储在本地存储中,然后将所有输入保存到一个csv文件中(应该保存在服务器上)是否可以将所有数据最后保存到一个csv文件中?

Using Blob ( https://developer.mozilla.org/en/docs/DOM/Blob ) this seems achievable. 使用Blob( https://developer.mozilla.org/en/docs/DOM/Blob )似乎可以实现。 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. 将文件获取到服务器上是另一回事,但是您应该可以对其进行调整并使用PHP,.NET或其他任何方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM