Hi All,
I know this is a known issue but I'm having difficulty on fixing my problem. It seems that I don't receive anything from my UI5 Application when I sent an image via FileUploader to my server. I am new to HCP and this is my first time handling XSJS file. I hope you can help 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 could not be 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 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); }
My code was built based on the tutorials I have found on the internet. Thank You.