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

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

Closed
jedmarcnocum opened this issue Nov 16, 2018 · 1 comment

Comments

@jedmarcnocum
Copy link

jedmarcnocum commented Nov 16, 2018

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.

@raymondfeng
Copy link
Contributor

I wonder if your previous version set Content-Type correctly or not.

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