Skip to Content

SAP UI5 onload function while uploadinig csv file to HANA DB table

HI Friends

I am trying to upload csv file into HANA DB table. how ever while using onload function i am not able to make function call in order to push json variable as a parameter into other method where service call will be made to to HANA DB and record should be pushed.

1) if i use service call in onload function json variable is getting override and only 1 record is getting inserted.

2) was not able to call other method in order to avoid the issue

could you please suggest me on this

onUpload: function(oEvent){

pst=this.getView().byId('pid');

var oFileUploader = this.getView().byId('FUP');

var domRef = oFileUploader.getFocusDomRef();

var record =[]; var rowData = []; var data ; var gval= oFileUploader.getValue().toString();

var file = oFileUploader.oFileUpload.files[0];

var that = this; this.filename = file.name; this.filetype = file.type; this.len=0;

// Reading a CSV file

var reader = new FileReader();

reader.readAsBinaryString(file);

reader.onload = function(e) {

var strCSV = e.target.result;

var rows = strCSV.split("\n");

var len = rows.length-1;

for(var i=0;i<this.len-1;i++){

console.log(this.len); debugger;

rowData=data[i+1].split(",");

if(rowData.length == 5){ //coulumns are 5

var dateStr = rowData[2];

var dateStrB = rowData[3];

jQuery.sap.require("sap.ui.core.format.DateFormat");

var oDateFormat = sap.ui.core.format.DateFormat.getDateInstance({pattern: 'yyyy/MM/dd'});

var TZOffsetMS = new Date(0).getTimezoneOffset()*60*1000;

var parseDate = new Date(oDateFormat.parse(dateStr).getTime()-TZOffsetMS);

var parseDateB = new Date(oDateFormat.parse(dateStrB).getTime()-TZOffsetMS);

var newData = {

"STR":rowData[0],

"SK_PHASE":rowData[1],

"BEGIN_DATE": parseDate,

"END_DATE":parseDateB,

"IPHASE":rowData[4] }

}

this.uploadExcel(newData);//failing unable to call uploadExcel method or function

}

};

},

thanks

Vijay

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    Posted on Feb 20, 2019 at 04:16 PM

    Hi bl vijaykumar,

    You wont be able to access "this" in the reader.onload, you can do it in the below two ways

    1. use jQuery.proxy

    reader.onloadend = jQuery.proxy(function () {
    				// your code
    				}, this);

    2. use bind(this)

    reader.onloadend = function () {
    				// your code
    				}.bind(this);

    3. pass the "this" reference to some variable

    var that = this;				
    reader.onloadend = function(e){
       that.yourMethod();
    };
    

    BR,

    Mahesh

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Feb 20, 2019 at 06:01 PM

    Hi Vijay Kumar,

    Yes, this scope is not available inside the File reader onload method. Sample code is available here.

    Thanks,

    Naga

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.