Skip to content

Equivalent of res.download() (ExpressJS) in the controlller for downloading files #2037

@jedmarcnocum

Description

@jedmarcnocum

Description / Steps to reproduce / Feature proposal

This is not an issue but rather a question, I did searched for possible answers with what I am trying to achieve but with no luck. I was trying to implement a simple route in the API that when a user visits the link, it will trigger a download action (PDF file) on their browser

I followed @loopback/example-shopping and managed to create a rest service running in Express similar to the example project. the rest service returns res.download(filePath) as response

Current Behavior

accessing the URL from loopback 4 controller opens the PDF viewer with empty content. But accessing the rest service directly (ExpressJS with res,download() as response) works fine

Expected Behavior

Similar behavior with ExpressJS res.download()

Hopefully someone can steer me to the right direction. Thank you!

EDIT:

I finally managed to make it work by injecting the RestBindings in the controller's constructor

constructor(
    @inject(RestBindings.Http.RESPONSE) public response: Response,
) {}

we can then simply return this.response.download(filePath); in the route function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions