cancel
Showing results for 
Search instead for 
Did you mean: 

How to upload files to HANA table using XSJS application

Former Member
0 Kudos

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);
}
former_member102415
Discoverer
0 Kudos

Hi,

I am trying figure this out. Have you finally find the problem of this implementation?

Thank you in advance!

NM.

shivam_bedwal
Explorer
0 Kudos

Hi,

How have you been able to pass relative URL of the xsjs file in UI5 application.

var uploadUrl ="/equip/oDataService/uploadFile.xsjs?filename=" + fileName;

Can you provide some details as how this url gets converted to the absolute URL of xsjs file.

Thanks,

Shivam

Accepted Solutions (0)

Answers (1)

Answers (1)

SergioG_TX
Active Contributor
0 Kudos

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