cancel
Showing results for 
Search instead for 
Did you mean: 

How to determine filename in CAP media entities?

geert-janklaps
Active Contributor

Hi,

I'm looking into downloading files using CAP. I'm using a custom handler to download the files (files get fetched from a C4C system using OData). The file download is working except for the fact that the filename is not set and I have to rename the file manually to be able to open it.

Example URL: /ServiceRequestAttachmentFolder('00163E10B4641ED89BF79ACCC3D13BD4')/Binary

This results in following download:

If I rename this file to e.g. test.pdf, I'm able to open the file.

Any ideas on how to set the actual filename? (e.g. attribute on result set, custom header, ...)

Thanks in advance!

Best regards,

Geert-Jan Klaps

UPDATE:

Solved this with following code for now (not the most ideal way since we're manipulating the headers manually, this is something the framework should be able to handle I guess):

req._.odataRes.setHeader('Content-Disposition',`attachment; filename="${result.Name}"`);
JérémyChabert
Participant
0 Kudos

Hi,

Do you have, by chance, the service hosted somewhere on Github or could you provide it as a sample of code following my comment ? Especially the part where you handle the answer from your remote server and redirect it to you oData answer in the handler.

We are trying to do the same, so far, we only download an empty file but I can't see where is the flaw in our code.

Here's a sample of the answer

{
    "d": {
        "ID": "20885163",
        "type": "application/vnd.ms-excel",
        "mediaType": "application/vnd.ms-excel",
        "fileName": "toto.xls",
        "__metadata": {
            "type": "API.Document",
            "uri": "http://localhost:4004/v2/api/Document('20885163')",
            "media_src": "http://localhost:4004/v2/api/Document('20885163')/$value",
            "content_type": "application/vnd.ms-excel"
        }
    }
}


Accepted Solutions (1)

Accepted Solutions (1)

OliverKlemenz
Advisor
Advisor

OData content disposition annotation was introduced recently
https://oasis-tcs.github.io/odata-vocabularies/vocabularies/Org.OData.Core.V1.html#contentdispositio...

this annotation needs to be adopted by CDS runtime...

anne.keller FYI..

gregorw
Active Contributor

Hi Oliver, Hi anne.keller,

any news when the runtime will support that annotation?

Best regards
Gregor

vansyckel
Advisor
Advisor
0 Kudos

Hi Gregor,

Sorry, no ETA yet.

Best,
Sebastian

Answers (3)

Answers (3)

mariya_yordanova
Explorer

Hi 8d8214c7f9734f45be69f95cc0d5aeee,

@Core.ContentDisposition annotation is supported in our last release version cds 5.1.5.

See also https://cap.cloud.sap/docs/guides/providing-services#media-data.

Best regards

Maria

gregorw
Active Contributor
0 Kudos

Dear Maria,

I can not confirm that this is working correctly. My bookshop-demo was already at CDS 5.1.5. My Entity Images in db/schema.cds#L108 is annotated like that:

entity Images : cuid, managed {
  @Core.MediaType                   : mediatype
  @Core.ContentDisposition.Filename : filename
  content   : LargeBinary;
  @Core.IsMediaType                 : true
  mediatype : String;
  filename  : String;
}

when testing this with tests/admin-images.http#L62 I get the header line:

content-disposition: inline; filename="Test.xlsx"

only from the OData V2 Service. When I cange the annotation to

@Core.ContentDisposition: filename

as mentioned in the documentation I get the following error:

And when I test this also the OData V2 Proxy doesnt regurn the content-disposition anymore.

Do I need to file an incident?

Best regards
Gregor

mariya_yordanova
Explorer
0 Kudos

Hi Gregor,

please file an incident.

Thanks and regards

Maria

gregorw
Active Contributor
0 Kudos

The Incident ID is 428102 / 2021

vitaly_kozyura
Participant

Hi,

Implementing this feature (as proposed by Oliver Klemenz) is now on agenda for node.js runtime.

We will inform you so far the feature is released.

Best regards,

Vitaly

gregorw
Active Contributor
0 Kudos

Hi Vitaly,

do you have an update when this feature will be shipped? I've updated my sample project bookshop-demo with the annotation @Core.ContentDisposition.Filename:

entity Images : cuid, managed {
  @Core.MediaType                   : mediatype
  @Core.ContentDisposition.Filename : filename
  content   : LargeBinary;
  @Core.IsMediaType                 : true
  mediatype : String;
  filename  : String;
}

in db/schema.cds#L123. But when testing using the REST Client script tests/admin-images.http#L62 I see the header:

content-disposition: inline; filename="Test.xlsx"

Only when doing the call via the V2 proxy.

Best regards
Gregor

OliverKlemenz
Advisor
Advisor
0 Kudos

Hi Gregor,

when using the CDS OData V2 Adapter Proxy, you can already use the annotation today. For OData V4, Anne can give you additional infos.

Best regards,

Oliver

gregorw
Active Contributor
0 Kudos

Hi Oliver,

thank you for this hint. The V2 Proxy works like a charm.

Best regards
Gregor