Skip to Content
2
Former Member
Feb 25, 2015 at 12:39 PM

File Upload from Controller to Server side xsjs

346 Views

Using the unified FileUploader I am having some issue with receiving the file uploaded,

my Scenario is SAPUI5 XML Views, SAP HANA SP07

I have seen the oDATA save by Peter Marcely but that has not helped me since its GW

My Codes are pasted below

XML VIEW

/// truncated <VBox width="70%">

<u:FileUploader

id="FileLoader"

width="100%"

tooltip="Upload picture of contact"

uploadComplete="handleUploadComplete"

change="handleValueChange"

typeMissmatch="handleTypeMissmatch"

style="Emphasized"

fileType="jpeg,jpg,png"

placeholder="Choose a file for Upload..."> </u:FileUploader>

<Button

text="Upload File"

press="handleUploadPress"/>


</VBox>

Controller.JS

handleUploadPress : function(oEvent)

{

var uploadField =document.getElementById("UpdateContact--FileLoader-fu");

var file = uploadField.files[0];

var reader = new FileReader();

reader.onload = (function(theFile) {

return function(e) {

var uploadUrl = "services/PictureFileUpload.xsjs?file_name="+name;

$.ajax({

url: uploadUrl,

type: "GET",

beforeSend: function(xhr) {

xhr.setRequestHeader('X-CSRF-Token',

$('meta[name="csrf-token"]').attr('content'))

},

success: function(data, textStatus, XMLHttpRequest){

reader.onload = null;

var token = XMLHttpRequest.getResponseHeader('X-CSRF-Token');

$.ajax({

url: uploadUrl,

type: "POST",

processData :false ,

contentType: false ,

data: file,

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.getCore().byId("FileUploader").getController().entryCount(data); }

oModel = new sap.ui.model.json.JSONModel("services/MY_FILE_UPLOAD_TABLE.xsodata/FILE_UPLOAD_TABLE?$format=json",true);

},

error: function(data, textStatus, XMLHttpRequest) {

sap.ui.commons.MessageBox.show("File could not be uploaded.", sap.ui.commons.MessageBox.Icon.ERROR, "Error");

}

});

},

error: function(data, textStatus, XMLHttpRequest) {

sap.m.MessageToast.show("Error Uploading file: "+data);

} }) ; } })(file);

reader.readAsDataURL(file); ///////////////////////////////////////////// } ,

PictureFileUpload.XSJS

var ID = 13; var IMAGE_NAME ="image Name"; try { var data = '', conn = $.db.getConnection(), pstmt; if($.request.entities.length>0){ data = $.request.body.asArrayBuffer(); var select_all_sales_orders_query = "UPDATE \"USER\".\"IMAGETABL\" SET IMAGE_CONTENT = ? "+

" where ID='"+ID+"'"; var pstmt = conn.prepareStatement( select_all_sales_orders_query); pstmt.setBlob(1,data); pstmt.execute();

$.response.contentType = 'text/plain';

$.response.setBody('Upload ok');

$.response.status = 200; }

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); }