I am creating a node.js app in which user can upload files and can download later. I am storing file information (original file name that user uploaded, ...) in mongodb document and named that file same as mongodb document id. Now i want my user to be able to download that file with the original file name.
What i want to know is when a user sends a GET request on http://myapp.com/mongoDocument_Id user gets a file named myOriginalfile.ext
I know about node-static and other modules but i can't rename them before sending file.
i am using koa.js framework.
Here's a simple example using
koa-file-server
:In short:
./static
using the MongoDB id as their filenametest.txt
)./static/123456
is offered as a download using the original filename set in theContent-Disposition
header (by usingthis.attachment(filename)
), which will make the browser store it locally astest.txt
instead of123456
.