cancel
Showing results for 
Search instead for 
Did you mean: 

Import text file from JS to XSJS

Former Member
0 Kudos

Hello,

XS users,

I'm trying to insert data into a table by importing a txt file.

but I can't get the file imported in the XSJS.

JS file using SAPUI5 FileUploader

var oFileUploader2 = new sap.ui.commons.FileUploader({

                    name: "upload2",

                    uploadOnChange: false,

                    uploadUrl: "../WebContent/vendas/ficheiros"});

                    layout2.createRow(oFileUploader2);

                    // create a second button to trigger the upload

                    var oTriggerButton = new sap.ui.commons.Button({

                    text:'Carregar',

                    press: buttonPressed

});

The buttonPress function is going to call our XSJS file

function buttonPressed(oControlEvent) {

      var jURL = '../WebContent/vendas/fileUploader.xsjs';

           jQuery.ajax({

                 url:jURL,

                 TYPE: 'POST'

             });

Now trying to get the file imported, i think that the problem it's that the body doesn't have the file

//create the query

var query = 'insert into "SCHEMA"."TABLE" values(?,?,?,?,?)';

//catch the body

if($.request.body){

    data = $.request.body.asString();

}

//open connection

var conn = $.db.getConnection();

try{

var pstmt = conn.prepareStatement(query);

var linhasInserir = data.split(/\r\n|\n/);

var lines;

var entries;

var cells;

pstmt.setBatchSize(linhasInserir.length);

for (lines=0; lines<linhasInserir.length; lines++)

{

       entries = linhasInserir[lines].split(';');

       cells = entries.splice(0,5);

       pstmt.setString(1,cells[0]);

       pstmt.setString(2,cells[1]);

       pstmt.setString(3,cells[2]);

       pstmt.setString(4,cells[3]);

       pstmt.setString(5,cells[4]);

           

      pstmt.addBatch();

}

pstmt.execute();

pstmt.close();

conn.commit();

conn.close();

Can anyone give some hit, how can we pass a file into the XSJS.

The code it´s working if I manualy set the data to be inserted

var data = [ "cell1;cell2;cell3;cell4;cell5","cell6;cell7;cell8;cell9:cell10"] the records will be well inserted into the hana table

thanks in advance,

Nuno Lopes

Accepted Solutions (1)

Accepted Solutions (1)

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

A file upload is a multipart entity and therefore you won't find it directly in the body of the request.  Try the following instead:

content = $.request.entities[0].body.asString();

You should find the file attachment in the first entity.

Former Member
0 Kudos

Hello Thomas,

I've tried the command u suggest

data = $.request.entities[0].body.asString();

But im getting error 500 when invoking the xsjs file:

500 Internal Server Error

Thanks in advance,

Best Regards

Nuno Lopes

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

First error 500 doesn't really tell you much.  Turn on Developer Mode on the XS Engine to be able to get detailed error messages. 

Second, why isn't the target of your FileUpload an xsjs service? The service can't process the upload if it isn't the target.

  1. var oFileUploader2 = new sap.ui.commons.FileUploader({ 
  2.                     name: "upload2"
  3.                     uploadOnChange: false
  4.                     uploadUrl: "../WebContent/vendas/ficheiros"}); 
  5.                     layout2.createRow(oFileUploader2); 
Former Member
0 Kudos

Hello Thomas,

Thanks for your answer.

I've chage uploadUrl do point to my xsjs file.

uploadUrl: "../WebContent/vendas/fileUploader.xsjs"

The problem is that the $.request.body is coming NULL.

Any idea what I'm doing wrong?

Thanks in advance,

Best Regards,

Nuno Lopes

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

Yes on file upload the $.request.body probably is null.  Are you trying as I suggested earlier:
$.request.entities[0].body.asString();

Former Member
0 Kudos

hello Thomas,

yes I'm doing

data = $.request.entities[0].body.asString();

Thanks in advance,

Best regards

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

Maybe you should detail what you do have, are you still not using uploadOnChange?  If you are using a button to trigger the upload then are you firing the fileUpload event from within the button event?  Here is such an example:

  var fileName = sap.ui.getCore().byId('FileName_Field').getValue();

                                         var repoPath = sap.ui.getCore().byId('RepoPath_Field').getValue();

 

                                         oFileUploader.setUploadUrl('/playground/fileUploader/services/upload.xsjs?cmd=uploadFileToRepo'+

                                                            '&FileName='+escape(fileName)+

                                                            '&RepoPath='+escape(repoPath));

                                         oFileUploader.attachUploadComplete(oEvent, oController.onUploadSuccess(oController) );

                                         oFileUploader.upload();

Former Member
0 Kudos

Hello Thomas,

Thanks for your help.

I was able to get the file and upload the data correctly.

I've changed, instead of call a function it will call the XSJS, i will use the file upload() as you suggest in the previous post

var oTriggerButton = new sap.ui.commons.Button({

               text:'Carregar',

               press:function() {

               oFileUploader2.upload();

                              }

               });

using this with the fileuploader created this way:

var oFileUploader2 = new sap.ui.commons.FileUploader({

          name: "upload2",

          uploadOnChange: false,

          uploadUrl: "../WebContent/vendas/fileUploader.xsjs"});

I was able to get the data in the element[0] and correctly insert into the hana database.

Thanks for the help.

Best Regards,

Nuno Lopes

Former Member
0 Kudos

Hi Thomas,

I am using the method you described to pass the file name and file type as parameters and pass the file content as part of the body. Below is the code to pass to the xsjs script

// create a second button to trigger the upload

            var oTriggerButton = new sap.ui.commons.Button({

            text:'Upload',

            press:function() {

                     var fileName = sap.ui.getCore().byId('fname').getValue();

                var fileType = sap.ui.getCore().byId('ftype').getValue();

                oFileUploader2.setUploadUrl('/xstrial/sample/services/fileUpload.xsjs?'+

                        'fName='+escape(fileName)+

                        '&fType='+escape(fileType));

            // call the upload method

            oFileUploader2.upload();

However I keep receiving a "405 method not allowed" error. Does something need to be done to enable the xsjs script to accept POST?

Thanks,

Sharan

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

I didn't do anything special to get mine working. 

Former Member
0 Kudos

Hi Thomas,

Thanks for your response. Not sure if this was caused due to the xsjs script being in a SAPUI5 project.

I moved xsjs script to an XSJS project instead and now the call goes through. However (and hopefully this is the last question), when I use the SAPUI5 fileuploader and send the file across to the xsjs script, I am able to see $.request.parameters but not $.request.entities or $.request.body

I took a look at what was coming in to the XSJS script using the debugger and below is the screenshot of all the parameters I see. Is there any reason why I am not seeing $.request.entities?

Thanks a bunch in advance.

Cheers,

Sharan

Former Member
0 Kudos

I probably had a typo when I used the statement the first time. Anyways it works now with just basic text files.

Thanks,

Sharan

Answers (0)