6

我正在尝试在 HANA XS 服务器上的 UI5 应用程序中实现文件上传。我找不到很多信息如何做到这一点 - 有人有想法吗?

4

2 回答 2

6

这是纯文本文件上传的简单实现:

客户端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);
}

这是服务器端 xsjs 服务:

    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;
}
于 2013-07-01T13:31:35.917 回答
0

XS 没有“即用型”服务允许您这样做。您当然可以在 HANA DB 中创建一个表,创建一个列类型的 BLOB,然后在 XS 上构建允许您从前端上传文件的服务。我希望这会有所帮助。

于 2013-05-29T20:40:59.057 回答