20

Is it possible to upload file using

<input type="file">

and then parse it to javascript array without saving the file on server-side? Could you advice any plugin to do it with csv (or any other) file type.

Is it good idea to do things such way without using back-end?

Thanks.

3

1 Answer 1

36

Js code for upload csv and parse it client side and then plot them into console

function uploadDealcsv () {}; 

  /*------ Method for read uploded csv file ------*/
  uploadDealcsv.prototype.getCsv = function(e) {
       
      let input = document.getElementById('dealCsv');
      input.addEventListener('change', function() {

        if (this.files && this.files[0]) {

            var myFile = this.files[0];
            var reader = new FileReader();
            
            reader.addEventListener('load', function (e) {
                
                let csvdata = e.target.result; 
                parseCsv.getParsecsvdata(csvdata); // calling function for parse csv data 
            });
            
            reader.readAsBinaryString(myFile);
        }
      });
    }

    /*------- Method for parse csv data and display --------------*/
    uploadDealcsv.prototype.getParsecsvdata = function(data) {

        let parsedata = [];

        let newLinebrk = data.split("\n");
        for(let i = 0; i < newLinebrk.length; i++) {

            parsedata.push(newLinebrk[i].split(","))
        }

        console.table(parsedata);
    }


  
  var parseCsv = new uploadDealcsv();
  parseCsv.getCsv();
<input type="file" id="dealCsv"/>

Sign up to request clarification or add additional context in comments.

1 Comment

Can also use reader.readAsText instead, if setting an encoding method is required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.