簡體   English   中英

從HTML輸入的數據生成CSV文件

[英]CSV file generation from HTML inputted data

我試圖以HTML格式將用戶輸入的數據傳遞給本地驅動器上的csv文件。 但是似乎有些錯誤。

我嘗試過下面的代碼,甚至是從其他來源復制過來的代碼。

    <html>
  <head>
  <script language="javascript">
    function WriteToFile(passForm) {
      var fso = new ActiveXObject("Scripting.FileSystemObject");
      var fileLoc = "C:\\sample.txt";
      var file = OpenTextFile(fileLoc,2,true,0);
     file.write("File handling in Javascript");
      file.Close();
      alert('File created successfully at location: ' + fileLoc);
     }
  </script>
  </head>
  <body>
  <p>create a csv file with following details -</p>
  <form>
    Type your first name: <input type="text" name="FirstName" size="20"><br>
    Type your last name: <input type="text" name="LastName" size="20"><br>
    <input type="button" value="submit" onclick="WriteToFile(this.form)">
  </form>
  </body>
</html>
</br></br></html>

無法創建CSV文件,並且HTML中無法出現任何錯誤。

您沒有使用變量fso調用OpenTextFile,還應記住IE有權在路徑中寫入

這應該為您工作:

<html>
<head>
    <script language="javascript">
        function WriteToFile(passForm) {
            var fso = new ActiveXObject("Scripting.FileSystemObject");
            var fileLoc = "C:\\Users\\yourUser\\sample.csv";
            var file = fso.OpenTextFile(fileLoc,2,true,0);
            file.write("File handling in Javascript");
            file.Close();
            alert('File created successfully at location: ' + fileLoc);
        }
    </script>
</head>
<body>
    <p>create a csv file with following details -</p>
    <form>
        Type your first name: <input type="text" name="FirstName" size="20"><br>
        Type your last name: <input type="text" name="LastName" size="20"><br>
        <input type="button" value="submit" onclick="WriteToFile(this.form)">
    </form>
</body>
</html>

您還可以使用適用於多種瀏覽器的此解決方案:

<html>
<head>
    <script language="javascript">
        function downloadCSV(form) {
            const content = 'Write your csv content here!!';
            const mimeType = 'text/csv;encoding:utf-8';
            const fileName = 'download.csv';
            const a = document.createElement('a');

            if (navigator.msSaveBlob) { // IE10
                navigator.msSaveBlob(new Blob([content], {
                    type: mimeType
                }), fileName);
            } else if (URL && 'download' in a) { //html5 A[download]
                a.href = URL.createObjectURL(new Blob([content], {
                    type: mimeType
                }));
                a.setAttribute('download', fileName);
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
            } else {
                location.href = 'data:application/octet-stream,' + encodeURIComponent(content); // only this mime type is supported
            }
        }
    </script>
</head>
<body>
    <p>create a csv file with following details -</p>
    <form>
        Type your first name: <input type="text" name="FirstName" size="20"><br>
        Type your last name: <input type="text" name="LastName" size="20"><br>
        <input type="button" value="submit" onclick="downloadCSV(this.form)">
    </form>
</body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM