Search code examples
odatasapui5urisap-gateway

OData - GET_STREAM Method to download an attachment file didn't triggered from SAPUI5 Controller


Background

I'm working with UploadSet component to upload, display and download attachment from GOS; to get it there I'm following this tutorial by ABAP Police although there's a little bit adjustment I made to get the result I wanted it. Everything is working fine before and I've already been able to upload, display and download.

All implementations of Odata service and Bapi strictly follow the method shown by ABAP Police, except for how to display and download the attachment file. The code below is how I download attachments using the UploadSet component.

SAPUI5 View

<mvc:View
     controller="..controller.activity"
     xmlns="sap.m"
     xmlns:mvc="sap.ui.core.mvc"
     xmlns:upload="sap.m.upload">
     ...
     <upload:UploadSet
         id="uploadActivityAttachment"
         class="uploadActivityAttachment"
         instantUpload="true"
         uploadUrl="/sap/opu/odata/sap/ZTEST_ATTACHMENT_2_SRV/attachmentSet"
         mode="MultiSelect"
         multiple="true"
         beforeUploadStarts="onBeforeUploadStarts"
         uploadCompleted="onUploadCompleted"
         items="{attachmentData>/attachment}">
         <upload:toolbar>
              <OverflowToolbar>
                    <ToolbarSpacer />
                    <Button 
                        id="downloadSelectedButton"
                        text="Download selected"
                        press="onDownloadSelectedButton" />
              </OverflowToolbar>
         </upload:toolbar>
         <upload:items>
              <upload:UploadSetItem
                     fileName="{attachmentData>FileName}"
                     mediaType="{attachmentData>MimeType}"
                     enabledEdit="false"
                     visibleEdit="false"
                     enabledRemove="true"
                     visibleRemove="true"
                     removePressed="onRemoveAttachment"
                     thumbnailUrl="{attachmentData>ThumbnailUrl}">
                     <upload:statuses>
                          <ObjectStatus
                              title="Document Id"
                              text="{attachmentData>DocumentId}"
                              visible="false" />
                          <ObjectStatus
                              title="Uploaded by"
                              text="{attachmentData>CreatorName}" />
                          <ObjectStatus
                              title="On"
                              text="{attachmentData>FormattedDateTime}" />
                     </upload:statuses>
              </upload:UploadSetItem>
         </upload:items>
     </upload:UploadSet>
...
</mvc:View>

SAPUI5 Controller

...
onDownloadSelectedButton: async function () {
    let itemDocId;
    const oUploadSet = this.getView().byId('uploadActivityAttachment');
    oUploadSet.getItems().forEach((item) => {
        (item.getListItem().getSelected()) {
         itemDocId = item.getStatuses()[0].getText();                  
         item.setUrl(`/sap/opu/odata/sap/ZTEST_ATTACHMENT_2_SRV/attachmentSet('${itemDocId}')/$value`);
         item.download(true);
    }
    });
},
...

The Problem

GET_STREAM Method is working fine before, until now. When I run the download function, I get the error message like this:

The request URI contains an invalid key predicate.

Here is the implementation of GET_STREAM Method in my Odata Service:

GET_STREAM Method

  METHOD /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM.

    DATA LS_STREAM  TYPE TY_S_MEDIA_RESOURCE.
    DATA(LV_ENTITY_SET_NAME) = IO_TECH_REQUEST_CONTEXT->GET_ENTITY_SET_NAME( ).
    DATA(LT_KEYS) = IO_TECH_REQUEST_CONTEXT->GET_KEYS( ).

    CASE LV_ENTITY_SET_NAME.
      WHEN 'attachmentSet'.
        DATA: LV_DOCUMENT_ID TYPE DOCUMENTID,
              LV_FILENAME    TYPE SO_OBJ_DES.
        LV_DOCUMENT_ID = LT_KEYS[ NAME = 'DOCUMENT_ID' ]-VALUE.
        CALL FUNCTION 'Z_DOWNLOAD_ATTACHMENT'
          EXPORTING
            IV_DOCUMENT_ID = LV_DOCUMENT_ID
          IMPORTING
            EV_VALUE       = LS_STREAM-VALUE
            EV_MIME_TYPE   = LS_STREAM-MIME_TYPE
            EV_FILENAME    = LV_FILENAME.

        DATA LS_LHEADER TYPE IHTTPNVP.
        LV_FILENAME = ESCAPE( VAL = LV_FILENAME FORMAT = CL_ABAP_FORMAT=>E_URL ).
        LS_LHEADER-NAME = 'Content-Disposition'.
        LS_LHEADER-VALUE = 'outline; filename=”' && LV_FILENAME && '”;'.
        SET_HEADER( IS_HEADER = LS_LHEADER ).

        COPY_DATA_TO_REF( EXPORTING IS_DATA = LS_STREAM
                          CHANGING  CR_DATA = ER_STREAM ).

    ENDCASE.
  ENDMETHOD.

Can anyone tell me, why can it suddenly get an error like what I mentioned above? whereas before it worked fine. Can you guys give me a suggestion? Thank you!

I've tried to find a solution by reading related question on sap community to the error message I got. But didn't get the answer that solve this my problem


Solution

  • As @JanSchulz said, in the Entity "Attachment" there are 3 keys, and what I put in the URL is only one, this problem has been resolved by changing the entity to only have one key. Thank you to everyone who has taken the time to help answer my problem.