vaibhav
vaibhav

Reputation: 23

CSV file generation from HTML inputted data

I was trying to pass user inputted data in HTML form to csv file on local drive. But seems some error in it.

I have tried below code even that is copied from other sources.

    <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 file is not getting created and cant get any error in HTML.

Upvotes: 1

Views: 106

Answers (1)

Ruben
Ruben

Reputation: 31

You were not using the variable fso to call OpenTextFile, you should also keep in mind that IE has permissions to write in the path

This should work for you:

<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>

You can also use this solution that works for several browsers:

<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>

Upvotes: 2

Related Questions