Forum Discussion

lucas's avatar
lucas
Creator II
5 years ago

how to read data from user selected file

Hello I'm currently trying to build a method to create parts from csv files selected by the user. I'm having issues with accessing the fileObject data which i need to then create my parts. I've tried different ways to read the file but it hasn't been succesfull.

here is my code:

var innovator = new Innovator();
var vlt = top.aras.vault;
alert("avant selectfile");
vlt.selectFile().then(function (fileObject)
{

read = new FileReader();                                        the part i'm having troubles with
//read.readAsText(fileObject);
reader.onload = function(){read.readAsText(fileObject);}
var fileContents= read.results;

var rows =fileContents.split("\n");
alert("apres rows split");
alert(rows[0]);

var i;

for (i = 0; i < rows.length(); i++)
{
//alert("entree n°"+ i+"dans la boucle");
var parts_to_add =rows.split(";");
alert(parts_to_add);
var partItem = innovator.newItem("Part","add");
partItem.setProperty("item_number", "test");
var Name =parts_to_add[0];
partItem.setProperty("name", Name);
var desc = parts_to_add[1];
partItem.setProperty("description", desc);
var resultItem = partItem.apply();
}
});

If you have any solution it would be greatly apreciated.

Thanks.

Lucas

3 Replies

  • read = new FileReader();                                        the part i'm having troubles with
    //read.readAsText(fileObject);
    read.onload = function(){read.readAsText(fileObject);}         ***
    var fileContents= read.results;

    • angela's avatar
      angela
      Catalyst II

      This is a variant that I previously used, also I used a native JS filepicker and not the inbuild one from Aras (my fileObject is slightly different). 

      var reader = new FileReader();
      if (reader.readAsBinaryString) {
        reader.onload = function (e) {
        ProcessFile(e.target.result);
      };
      reader.readAsBinaryString(fileUpload.files[0]);
      }

      I right now work on an alternative BatchLoader that can upload XLS, XLSX, CSV and TXT. It shall mainly be used for BOM import, but I also want to use it for all kind of imports for Aras instances without subscription.

      I search for people that want to join in the project. My current draft works and imports already. It can be flexible used for various kind of import and even contains a preview of the data. But it´s not performance optimized yet and validation before import is missing. If you or anyone else in community is interested in active (!) collaboration, you would be welcomed to join the project!

      • lucas's avatar
        lucas
        Creator II

        thanks for your help.

        var read = new FileReader();
        read.readAsText(fileObject);
        //read.onload = function(){read.readAsText(fileObject);}
        //var fileContents= read.results;
        read.onload = readerEvent =>
        {

        this fixed my problem. if anyone have the same problem.