如何用dojo uploader读取上传的csv文件而不将其保存在服务器中?

qlckcl4x  于 2022-12-16  发布在  Dojo
关注(0)|答案(1)|浏览(131)

我想导入CSV文件并读取其中的数据,但不将文件保存在服务器中。该怎么做?
我更喜欢使用Dojo,但如果不可能,我可以使用HTML输入类型文件。

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();
wr98u20j

wr98u20j1#

我决定了!

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]);
      }
   }
});

相关问题