Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unbound action which returns largebinary #1333

Open
ilhanburak opened this issue Oct 9, 2024 · 1 comment
Open

unbound action which returns largebinary #1333

ilhanburak opened this issue Oct 9, 2024 · 1 comment

Comments

@ilhanburak
Copy link

Hello,

If I add custom handler to Read entity with mediatype property, i can return binary result to client.

entity Books : managed {
  key ID : Integer;
  descr  : localized String(1111);
  genre  : Association to Genres;
  stock  : Integer;
  price  : Decimal;
  currency : Currency;
  image : LargeBinary @Core.MediaType : 'image/png';
}

And implementation of read event.

this.on("READ", Books, async (req, next) => {

      const url = req._.req.path
      if (url.includes('image')) {

        let file = fs.readFileSync(path.resolve(__dirname, '../image.png'));
        const readable = new Readable()
        const result = new Array()
        readable.push(file)
        readable.push(null)
        result.push({
          value: readable,
          $mediaContentType: "image/png",
          $mediaContentDispositionFilename: "image.png"
        })
        return result
      } else
        await next();
    })

But if I use same method in unbound action, response is not a file, it's json with following content. ( After renaming .png to txt)
{"@odata.context":"$metadata#CatalogService.return_CatalogService_downloadAllImages"}

Unbound action definition

function downloadAllImages()                                                           returns {
   @Core.ContentDisposition.Filename: fileName
   @Core.MediaType                  : mediaType
   value : LargeBinary;
   mediaType : String;
   fileName : String;
 };

Handler of unbound action.

this.on('downloadAllImages', async (req) => {

      let file = fs.readFileSync(path.resolve(__dirname, '../image.png'));
      const readable = new Readable()
      const result = new Array()
      readable.push(file)
      readable.push(null)
      result.push({
        value: readable,
        $mediaContentType: "image/png",
        $mediaContentDispositionFilename: "image.png"
      })
      return result
    })

How can I send file response to client in unbound action ?
Regards.

@renejeglinsky
Copy link
Contributor

Hi @ilhanburak ,
in this repository we handle issue related to documentation. I don't see any reference in your issue where exisiting documentation is linked which might be wrong, insufficient or else.
Overall my impression is, that you would get faster and better support when asking this question in the SAP Community.
Do you agree?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants