使用XSJS将映像插入SAP HANA表

时间:2016-02-08 01:49:28

标签: javascript sql image sap hana

我知道这是一个已知问题,但我在解决问题上遇到了困难。当我通过FileUploader将图像发送到我的服务器时,我的UI5应用程序似乎没有收到任何内容。我是HCP的新手,这是我第一次处理XSJS文件。我希望你能帮助我。

UI5.controller.js

sap.ui.define([  
"sap/ui/core/mvc/Controller"  
], function(Controller) {  
"use strict";  
return Controller.extend("sample.controller.View1", {  
handleUploadPress : function(oEvent)    
{    
    var fileLoader =this.getView().byId("FileLoader");//XML View  

         var fileName = fileLoader.getValue();    
         jQuery.sap.require("sap.ui.commons.MessageBox");    
           if (fileName === "" )    
             {    
                     sap.ui.commons.MessageBox.show("Please choose File.", sap.ui.commons.MessageBox.Icon.INFORMATION, "Information");    
           }    
           else    
           {    
             var uploadUrl = "https://xxxxxx/services/Sample.xsjs?file_name="+fileName;    
             var formEle = jQuery.sap.domById("UpdateContact--FileLoader");   
             var form = $(formEle).find("form")[0] ;    
             var fd = new FormData(form);       
             $.ajax({    
                       url: uploadUrl,    
                       type: "GET",    
                       beforeSend: function(xhr)    
                       {    
                                 xhr.setRequestHeader("X-CSRF-Token", "Fetch");    
                       },    
                       success: function(data, textStatus, XMLHttpRequest) {    
                                 var token = XMLHttpRequest.getResponseHeader('X-CSRF-Token');    
                                 $.ajax({    
                                           url: uploadUrl,    
                                           type: "POST",    
                                           processData :false ,   
                                           contentType: false,  
                                           data: fd,    
                                           beforeSend: function(xhr)    
                                           {    
                                                     xhr.setRequestHeader("X-CSRF-Token", token);    
                                           },    
                                           success: function(data, textStatus, XMLHttpRequest)     
                                           {    
                                                var resptext = XMLHttpRequest.responseText;    
                                                jQuery.sap.require("sap.ui.commons.MessageBox");    
                                                sap.ui.commons.MessageBox.show(resptext, sap.ui.commons.MessageBox.Icon.INFORMATION, "Information");    
                                               if(data === "Upload successful"){  
                                                  sap.ui.commons.MessageBox.show("File uploaded.", sap.ui.commons.MessageBox.Icon.INFORMATION, "Information");    
                                               }  
                                           },    
                                           error: function(data, textStatus, XMLHttpRequest)    
                                           {    
                                                     sap.ui.commons.MessageBox.show("File could not be uploaded.", sap.ui.commons.MessageBox.Icon.ERROR, "Error");    
                                           }    
                                 });    
                       }} ) ;    
              }    
         }  
       });  

XSJS服务:

$.response.contentType = "text/html";    
try    
{    
    var conn = $.hdb.getConnection();    
    var filename = $.request.parameters.get("file_name");   
    var headers = $.entity.headers.length;  
    var pstmt = conn.prepareStatement("INSERT INTO \"XXX_ASSETS\".\"XXX\" VALUES('1',?,'test',CURRENT_USER,CURRENT_TIMESTAMP)");   
    if($.request.entities.length > 0){    
       var file_body = $.request.entities[0].body.asArrayBuffer();    
       pstmt.setBlob(1,file_body);  
       pstmt.execute();  
       $.response.setBody("[200]:Upload successful!");    
    }    
    else    
    {    
       $.response.setBody("No Entries");    
    }    
    pstmt.close();    
    conn.commit();    
    conn.close();    

    }    
    catch(err)    
    {    
       if (pstmt !== null)    
       {    
         pstmt.close();    
       }    
       if (conn !== null)    
       {    
         conn.close();    
       }    
       $.response.setBody(err.message);    
     }   
} 

我的代码是基于我在互联网上找到的教程构建的。谢谢。

1 个答案:

答案 0 :(得分:0)

保存图像的一种好方法是转换(Base64)并将其另存为HANA表中的blob。

致谢