문제

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?

도움이 되었습니까?

해결책

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;
}

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top