公众号:matinal
本文作者:matinal
前言部分
大家可以关注我的公众号,公众号里的排版更好,阅读更舒适。
正文部分
玩就玩一个高级的,此篇文章包含了SAP UI5,SAP HANA,SAP XS HANA串联起来
业务需求:SAP UI5传输图片,最后存储到HANA中。
下面体验一下梦幻般的做法,O(∩_∩)O哈哈~
1、首先在HANA中创建一个表,用于存储
schema名:zmatinal。表名:zimage。表里两个字段分别是:name,content。
CREATE COLUMN TABLE "ZMATINAL"."ZIMAGE"( "NAME" NVARCHAR(100), "CONTENT" BLOB MEMORY THRESHOLD 1000, PRIMARY KEY ( "NAME" ) ) UNLOAD PRIORITY 5 AUTO MERGE;
2、用SAP UI5上传图片
其中xml配置
<u:FileUploader id="fileUploader" name="myFileUpload" uploadUrl="upload/" width="400px" tooltip="Upload your file to the local server" uploadComplete="handleUploadComplete"/> <Button text="Upload File" press="handleUploadPress"/>
其中controller配置
handleUploadPress: function(oEvent) { var oFileUploader = this.getView().byId("fileUploader"); oFileUploader.upload(); }
3、在XSJS中,只需将该请求读入ArrayBuffer并作为BLOB存储
var schema_name = "ZMATINAL"; var filename = $.request.parameters.get('filename'); try { var conn = $.db.getConnection(); var pstmt = conn.prepareStatement("INSERT INTO "ZMATINAL"."ZIMAGE" (NAME, CONTENT) VALUES (?, ?)"); if($.request.entities.length>0) { // Read in the posted image or binary data as an Array Buffer - you can use this to save as a BLOB var file_body = $.request.entities[0].body.asArrayBuffer(); pstmt.setString(1,filename); //设置文件名 pstmt.setBlob(2,file_body); //图片数据的数组缓冲区 pstmt.execute(); } else { $.response.setBody("没有请求条目"); } pstmt.close(); conn.commit(); conn.close(); $.response.contentType = "text/html"; $.response.setBody("[200]:上传文件" + filename + "成功!"); } catch(err) { if (pstmt !== null) { pstmt.close(); } if (conn !== null) { conn.close(); } $.response.contentType = "text/html"; $.response.setBody("文件无法保存在数据库中,出现错误:" + err.message); }