how to serve or force file download located from a different directory in meteor to client browser

830 views Asked by At

A user comes to my site and inputs something, and my site generates a file as an output.

Unfortunately i cannot place the generated file on the public directory - as you all now Meteor watches this and restarts every time the public folder content is changed.

so my generated files lives in .meteor/local/build/programs/server/files

so for example i have document.pdf that lives in that directory, I'd like to serve/force/trigger a file download to my client's browser that lets his browser download this document.pdf file.

2

There are 2 answers

3
Tarang On

In general Its not a very good idea to do this. It makes it very hard to scale your app. Node isn't good at serving chunky static files either.

Then also if you have two servers there is a slight chance that the other one's data is requested (e.g if you use a download manager).

I'm not sure but I think Meteor's live code reload doesn't work/is switched off in when in production mode (when using meteor deploy or meteor bundle)

The best thing to do would be to upload your file to S3 and then redirect the user to the file there.

0
MrMowgli On

You can also use Iron Router and server side routes to create a dynamic file download. See Iron Router Server Side docs. Then you set your content type to application/pdf and send back the file directly without saving it to the filesystem. If you need to you can also save it in some other folder and serve it up yourself.

Then have a peek at this answer for an example of reading in and streaming out a file: Node JS file downloads using a stream.

Since this is a server side route, using express and Iron Router, you shouldn't have to mess with any of the fibers related async issues.