Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SAP HANA XS File upload with UI5

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?

like image 628
user2345998 Avatar asked May 03 '13 07:05

user2345998


1 Answers

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;
}
like image 162
user2345998 Avatar answered Oct 24 '22 07:10

user2345998