How to read an uploaded csv file with dojo uploader without saving it in a server? -
i want import csv file , read data on without saving file in server. how can do?
prefer use dojo if isn't possible can use 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 file , use data } }, onchange: function() { console.log("file: ", this.getfilelist()); var file = this.getfilelist(); if (file[0].type != "text/csv"){ console.log("not csv file"); this.reset(); }else{ this.upload(file[0]); } } }, domconstruct.create("div", { style: "" }, this.toolbarnode)); this.import.startup();
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 input lines var lines = csv.split('\n'); // extract column names first line var columnnamesline = lines[0]; var columnnames = parse(columnnamesline); // extract data subsequent lines var datalines = lines.slice(1); var data = datalines.map(parse); // prints array of colomuns // console.log(columnnames); if (columnnames[0] == "code" && columnnames[1] == "description") { // prints data // console.log(data); var dataobjects = data.map(function(arr) { var dataobject = {}; columnnames.foreach(function(columnname, i) { dataobject[columnname] = arr[i]; }); return dataobject; }); // prints data object console.log(dataobjects); // final data } else { // notification: format error } } else { // notification: file format error } uploader.value = ""; }; fr.readasdataurl(e.target.files[0]); } } });
Comments
Post a Comment