on 07-10-2013 7:16 PM
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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();
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
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
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
User | Count |
---|---|
78 | |
10 | |
7 | |
6 | |
6 | |
6 | |
6 | |
6 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.