6

I'm trying to implement a file upload in a UI5 application on a HANA XS Server. I can't find many informations how to do that - somebody got an idea?

0

2 Answers 2

6

here's the simple implementation of a plain text file upload:

Client side js:

doUpload: function() {
    var uploadField = document.getElementById("ulUploader1-fu");
    var file = uploadField.files[0];

    var reader = new FileReader();
    reader.onload = function (event) {
    var source = event.target.result; // this is the binary values
    var name = file.name;      

    $.ajax({
        url: "/services/upload.xsjs?cmd=Import",
        type: "PUT",
        processData: false,
        contentType: file.type,
        data: source,
        xhr: function() {
            var req = $.ajaxSettings.xhr();
            if (req) {
                if (req.overrideMimeType) {
                    req.overrideMimeType('text/plain; charset=x-user-defined');
                }
                if (req.sendAsBinary) {
                    req.send = req.sendAsBinary;
                }
            }
            return req;
        },
        error: function(xhr, textStatus, errorThrown){
            alert(xhr.responseText);
        },
        success: function() {
            reader.onload = null;
        }
        });
    };
    reader.readAsText(file);
}

And here's the serverside xsjs service:

    function doImport() {
    var data = '', conn = $.db.getConnection(), pstmt;  

    if($.request.body){
        data = $.request.body.asString();     
    }

    var conn = $.db.getConnection();
    var pstmt = conn.prepareStatement( 'INSERT INTO "TEST"."UPLOAD" (ID, MIMETYPE, DATA) VALUES(?,?,?)' );

    pstmt.setInteger(1,1);
    pstmt.setString(2,"text/plain");
    pstmt.setString(3,data);

    pstmt.execute();
    pstmt.close();

    conn.commit();
    conn.close();

    doResponse(200,'');

    $.response.contentType = 'text/plain';
    $.response.setBody('Upload ok');
    $.response.status = 200;
}
2
  • What if I want to send array of json object? How can I access them on HANA? When I send data using ajax post I cant see any data on hana. For example - type: "PUT", processData: false, contentType: file.type, data: objectArray,Any suggestions?
    – user557657
    Commented Jul 24, 2017 at 15:28
  • my problem is two times the data is trying to add to the database while in ajax call Commented Dec 30, 2023 at 7:05
0

There is no "ready-to-consume" service from XS that allows you to do that. You can of course create a table in HANA DB, create a column-type BLOB and then build service on XS that allows you to upload file from your front-end. I hope that helps.

1
  • I did not get you. Could you include some more information? Commented Dec 30, 2023 at 7:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.