SAPUI5, NW ABAP Gateway (OData), SAP Fiori

Upload/download File in SAP UI5 Application using Gateway

Please find below step by step how you can upload and download file.

Please note that i am implementing this on Central Hub Gateway Deployment so will be explaining this in two sections

Section I: Back End

Step-1 : Create Table

First I created a table on back-end system that will contain the uploaded files.you can add many fields you want but your table must contain Filename,Value and mimetype.

SAPUI5, NW ABAP Gateway (OData), SAP Fiori

after creating table we developed three Function Modules.

Step-2: Function Modules

Please note all functions are RFC Enabled.

1. Upload

In this function you will import following parameters and store values in table.

SAPUI5, NW ABAP Gateway (OData), SAP Fiori

DATA: TEMP_WO LIKE ZBM_WORKORDERAPP-WORKORDER.
DATA: WA_UPLOAD TYPE ZBM_FILEUPLOAD.

* SELECT SINGLE WORKORDER INTO TEMP_WO FROM ZBM_WORKORDERAPP
* WHERE WORKORDER EQ IM_WORKORDER.
*
* CHECK TEMP_WO IS NOT INITIAL.

WA_UPLOAD-MANDT = SY-MANDT.
WA_UPLOAD-WORKORDER = IM_WORKORDER.
WA_UPLOAD-FILENAME = IM_FILENAME.
WA_UPLOAD-VALUE = IM_MEDIA_RESOURCE.
WA_UPLOAD-MIMETYPE = IM_MIME_TYPE.
WA_UPLOAD-DATESTAMP = SY-DATUM.
WA_UPLOAD-TIME = SY-UZEIT.

INSERT INTO ZBM_FILEUPLOAD VALUES WA_UPLOAD.
IF SY-SUBRC <> 0.
RAISE DATA_NOT_INSERTED.
ENDIF.

ENDFUNCTION.
please note this function module is for Create Odata service implementation

2. Get List of files

In my case user can add multiple files for specific workorder. To implement this functionality i created function module which returns table with file name and file type.

SAPUI5, NW ABAP Gateway (OData), SAP Fiori

please note this function module is for GetEntitySet Odata service implementation

3. Download

In this function i pass two parameters and get related file.

SAPUI5, NW ABAP Gateway (OData), SAP Fiori

please note this function module is for GetEntity Odata service implementation

Section II: Front-End SAP Gateway

Step-1: Service Creation

I have created a project in SEGW and an entity type based upload function

SAPUI5, NW ABAP Gateway (OData), SAP Fiori

following properties were created.

SAPUI5, NW ABAP Gateway (OData), SAP Fiori

Step-2 : Service Implementation

we will use mapping

SAPUI5, NW ABAP Gateway (OData), SAP Fiori

we have implement following services

SAPUI5, NW ABAP Gateway (OData), SAP Fiori

For Create we used upload function,

SAPUI5, NW ABAP Gateway (OData), SAP Fiori

for Get Entity we used download

SAPUI5, NW ABAP Gateway (OData), SAP Fiori

and for Get Entity Set we used Get file names function

SAPUI5, NW ABAP Gateway (OData), SAP Fiori

thats it !

lets generate.

SAPUI5, NW ABAP Gateway (OData), SAP Fiori

So we have implemented odata service successfully.

Section III: WebIDE

In this section we will see we can use above service in UI5 application

In this Ist we will upload the file to back end system,and the we will download file using UI5 application.

Step-1: Upload file using file uploader.

I will be using file uploader component.

SAPUI5, NW ABAP Gateway (OData), SAP Fiori

On upload i have implemented postFiletobackend

SAPUI5, NW ABAP Gateway (OData), SAP Fiori

we have used JavaScript function btoa for encoding the file content

question arise from where filename,filetype and content come from.below is the code how get details from uploader.

onUpload: function (oEvent) {
var oFileUpload = this.getView().byId(“fileUploader”);
var domRef = oFileUpload.getFocusDomRef();
var file = domRef.files[0];
var that = this;
this.fileName = file.name;
this.fileType = file.type;
var reader = new FileReader();
reader.onload = function (e) {var vContent = e.currentTarget.result.replace(“data:” + file.type + “;base64,”, “”);
that.postFileToBackend(workorderId, that.fileName, that.fileType, vContent);
};
reader.readAsDataURL(file);
}
once the file uploaded you will get success message

SAPUI5, NW ABAP Gateway (OData), SAP Fiori

Step-2: Download file

I didn’t use upload collection.Instead of that i have just list down the file related to workorder using list object.

and by using get entity set we will get following view.

SAPUI5, NW ABAP Gateway (OData), SAP Fiori

Now i have implemented download function on click of row.

SAPUI5, NW ABAP Gateway (OData), SAP Fiori

I have tried to use sap.ui.core.util.File.save() function but only getting blank or corrupt file.only text file was working fine.

To achieve the goal i have used native JavaScript functionality by creating blob from the file “Value” and open the blob by using url.

Leave a Reply

Your email address will not be published.