Search code examples
dojo

How to read an uploaded csv file with dojo uploader without saving it in a server?


I want to import a CSV file and read the data on it without saving the file in the server. How can I do?
I prefer to use Dojo but if it isn't possible I can use the HTML input type file.

this.import = new Uploader({
               label: "Import",
               showLabel: true,
               iconClass: "uploadBtn",
               multiple: false,
               uploadOnSelect: false,
               onBegin: function() {
                  progressDialog.show();
               },
               onProgress: function(rev) {
                  console.log("progress", rev);
                  if (rev.type === "load") {
                     progressDialog.close();
                     this.reset();
                     // READ THE FILE AND USE THE DATA
                  }
               },
               onChange: function() {
                  console.log("file: ", this.getFileList());
                  var file = this.getFileList();

                  if (file[0].type != "text/csv"){
                     console.log("not a csv file");
                     this.reset();
                  }else{
                     this.upload(file[0]);
                  }

               }
            }, domConstruct.create("div", {
               style: ""
            }, this.toolbarNode));
            this.import.startup();

Solution

  • I resolved!

    this.uploader = domConstruct.create("input", {
       type: "file",
       accept: ".csv",
       change: function(e) {
          // console.log(e);
          var uploader = this;
          if (e.target.files && e.target.files[0]) {
             var FR = new FileReader();
             FR.onload = function(up) {
                var format = up.target.result.substring(up.target.result.indexOf("/") + 1, up.target.result.indexOf(";"));
                if (format == "csv") {
                   var base64 = up.target.result.substring(up.target.result.indexOf(",") + 1, up.target.result.length);
                   var csv = window.atob(base64);
    
                   // Split the input into lines
                   var lines = csv.split('\n');
                   // Extract column names from the first line
                   var columnNamesLine = lines[0];
                   var columnNames = parse(columnNamesLine);
                   // Extract data from subsequent lines
                   var dataLines = lines.slice(1);
                   var data = dataLines.map(parse);
                   // Prints the array of the columns
                   // console.log(columnNames);
                   if (columnNames[0] == "Code" && columnNames[1] == "Description") {
                      // Prints the data
                      // console.log(data);
                      var dataObjects = data.map(function(arr) {
                         var dataObject = {};
                         columnNames.forEach(function(columnName, i) {
                            dataObject[columnName] = arr[i];
                         });
                         return dataObject;
                      });
                      // Prints the DATA object
                      console.log(dataObjects); // FINAL DATA
    
                   } else {
                      // NOTIFICATION: format error
                   }
    
                } else {
                   // NOTIFICATION: file format error
                }
                uploader.value = "";
             };
             FR.readAsDataURL(e.target.files[0]);
          }
       }
    });