javascript - Inserting image to SAP HANA Table using XSJS -
i know known issue i'm having difficulty on fixing problem. seems don't receive ui5 application when sent image via fileuploader server. new hcp , first time handling xsjs file. hope can me.
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 not uploaded.", sap.ui.commons.messagebox.icon.error, "error"); } }); }} ) ; } } });
xsjs service:
$.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 \"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); } }
my code built based on tutorials have found on internet. thank you.
Comments
Post a Comment