on 10-13-2017 7:39 AM
Hi Experts ,
I am developing a sap ui5 application where i need to upload image/pdf files.
I am using file uploader control ,but couldn't able to pass the file contents to the XS application.
View Code :
<u:FileUploader id="fileUploader" name="myFileUpload" uploadUrl="" width="400px" tooltip="Upload your file to the local server"
uploadComplete="handleUploadComplete"/>
<Button text="Upload File" press="handleUploadPress"/>
Controller Code
handleUploadPress: function(oEvent) {
var fileLoader = this.getView().byId("fileUploader");
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 = "/equip/oDataService/uploadFile.xsjs?filename=" + fileName;
var file = jQuery.sap.domById(fileLoader.getId() + "-fu").files[0];
$.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: 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");
},
error: function(data, textStatus, XMLHttpRequest) {
sap.ui.commons.MessageBox.show("File could not be uploaded.", sap.ui.commons.MessageBox.Icon.ERROR, "Error");
}
});
}
});
}
},
XSJS Application code
try {
var fileName = $.request.parameters.get("filename");
} catch (e) {
$.response.status = $.net.http.INTERNAL_SERVER_ERROR;
$.response.setBody(e.message);
}
var output = {};
output.data = [];
try {
var conn = $.db.getConnection();
var pstmt = conn.prepareStatement(
"INSERT INTO \"EQUIPMENT_LOG\".\"LOG_ENTRY_ATTACHMENT\" (LOG_ID, ATTACHMENT, ATTACHMENT_DESC) VALUES (?, ?, ?)");
if ($.request.entities.length > 0) {
var logID = 3;
logID = parseInt(logID);
// Read in the posted image or binary data as an Array Buffer - you can use this to save as a BLOB
var fileBody = $.request.entities[0].body.asArrayBuffer();
pstmt.setInteger(1, logID);
pstmt.setBlob(2, fileBody); // Set the Blob as the array buffer that has the image data
pstmt.setString(2, fileName);
pstmt.executeQuery();
conn.commit();
var record = [];
record.push(logID);
record.push(fileName);
output.data.push(record);
$.response.setBody(0);
conn.close();
$.response.contentType = "text/html";
$.response.setBody("[200]:Upload for file" + fileName + " was successful!");
} else {
$.response.setBody("No Entries in request");
}
} catch (e) {
$.response.status = $.net.http.INTERNAL_SERVER_ERROR;
$.response.setBody(1);
}
there are a few blogs out there for image upload -- here is oine
https://blogs.sap.com/2016/09/01/upload-and-retrieve-image-using-sap-hana-xs-sap-ui5/
as far as pdf.. im not sure if you can upload one the same way.. most likely you have to post it tere
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
90 | |
10 | |
10 | |
10 | |
7 | |
7 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.