cancel
Showing results for 
Search instead for 
Did you mean: 

CSV Upload and parse not working in HANA XSJS

Raja
Product and Topic Expert
Product and Topic Expert
0 Kudos

I am uploading a csv file to HANA system through XSJS. The XSJS will parse the CSV file and save the individual columns to a HANA table.

The file is uploaded using SAPUI5 FileUploader control.

SAPUI5 View:

<code><u:FileUploader id="fileUploader" name="myFileUpload" uploadUrl="/FileUpload.xsjs" uploadComplete="handleUploadComplete"/>
<Button text="Upload File" press="handleUploadPress"/>

SAPUI5 Controller

<code>var fileUploader = this.getView().byId("fileUploader");
fileUploader.upload();

XSJS Code:

<code>function escape(v1) {
    var v2 = v1.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
    return v2;
}
$.response.contentType = "text/html";
try {
    var conn = $.db.getConnection();

    var pstmtDelete = conn.prepareStatement("DELETE FROM  SCHEMA.\"TABLE1\"");
    var rs = pstmtDelete.executeUpdate();

    var pstmt = conn.prepareStatement("insert into SCHEMA.\"TABLE1\" " +
        "values(?,?,?, ?, ?, ?)");
    if ($.request.entities.length > 0) {
        var file_body = $.request.entities[0].body.asString();
        var allTextLines = file_body.split(/\r\n|\n/);
        var lines;
        var entries;
        var col;
        pstmt.setBatchSize(allTextLines.length - 1);
        for (lines = 1; lines < 2; lines++) {
                entries = allTextLines[lines].split(',',6);
                col = entries.splice(0, allTextLines.length);
                var i;
                for (i = 0; i < 6; i++) {
                    if (col[i] && col[i].length > 0) {
                        col[i] = escape(col[i]);
                        pstmt.setString(i + 1, col[i]);

                    }
                }
                 pstmt.addBatch();

            }
            pstmt.executeBatch();
            conn.commit();
            $.response.setBody("[200]:Upload successful!");
        } else {
            $.response.setBody("No Entries");
        }
        pstmt.close();
        conn.close();
    } catch (err) {
        if (pstmt !== null) {
            pstmt.close();
        }
        if (conn !== null) {
            conn.close();
        }
        $.response.setBody(err.message);
    }

And the Table schema is

<code>CREATE COLUMN TABLE "SCHEMA"."TABLE1" ("ID" NVARCHAR(20) NOT NULL ,
     "SystemNumber" NVARCHAR(20) NOT NULL ,
     "SystemId" NVARCHAR(20) NOT NULL ,
     "Account" NVARCHAR(20) NOT NULL ,
     "Name" NVARCHAR(20),
     "Dtail" NVARCHAR(200),
     PRIMARY KEY ("ID"))

The csv file can be found at https://drive.google.com/file/d/0B55hLjUUUzfmUGdlX2UtVlJoTVk/view?usp=sharing

Problem: When XSJS service is called, the XS engine crashes and gives 503 error. There is no error in the log too.

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

HI Raja Prasad,

Did you got any solution for this ?

I am trying same , uploading file from sap ui5 to hana tables using XSJS application.