Blake Lewis
Blake Lewis

Reputation: 113

Linking a file for download in Node - Javascript, EJS

I am trying to have one of my EJS pages have a button to download a file from my Node.js webapp. I am using Node.js, Express, Passport, etc... Currently, when I click the link I have, it links to a page that says Cannot GET /public/files/PRA.xlsx

Here is the code that I have for the link:

<a href="/public/files/PRA.xlsx" download>Download PRA File</a>

And here you can see the file structure of my app.

enter image description here

Any help or tips would be appreciated.

Upvotes: 0

Views: 2210

Answers (1)

Ryan Allred
Ryan Allred

Reputation: 414

Try: <a href="/files/PRA.xlsx" download>Download PRA File</a>

If you have the static middleware set up for express, it will mount the folder you specify as the location where files may be served from.

Example:

app.use(express.static('public'))

Will serve any files located in the public folder. If the public folder has a file like: image.png, you'll be able to get to it from localhost:3000/image.png.

If you want to serve that image from localhost:3000/public/image.png, you can do the following:

app.use('/public', express.static('public'))

There's more information on this in the express documentation. https://expressjs.com/en/starter/static-files.html

Upvotes: 1

Related Questions