cancel
Showing results for 
Search instead for 
Did you mean: 

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

bl_vijaykumar
Active Participant
0 Kudos

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

maheshpalavalli
Active Contributor
0 Kudos

you can put the code in the code editor as it will be formatted and easily understandably.

maheshpalavalli
Active Contributor
0 Kudos

Great!! But it's better to go with the 1st or 2nd option..

BR

Mahesh

Accepted Solutions (1)

Accepted Solutions (1)

maheshpalavalli
Active Contributor
0 Kudos

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

bl_vijaykumar
Active Participant
0 Kudos

Thanks mahesh kumar 3rd option helped me quickly

Answers (1)

Answers (1)

NagaPrakashT
Contributor
0 Kudos

Hi Vijay Kumar,

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

Thanks,

Naga