5

I have a local file download, which gets triggered in the following fashion.

panelBody.append('<button onclick="downloadCsv(\''+skl+'\')">Download csv</button>')
function downloadCsv(data){
 var filename=data+'_report.csv'
 var form = $('<form>', {action: '/admin/download', method: 'GET'});
 form.append($('<input>', {name: 'file_name', value: filename}));
 form.submit();
 return false;
}
router.get('/download',helper.isSchoolAdmin,feedController.downloadCsv);
downloadCsv:function(req,res){
 var fileName = process.cwd()+'/reports/'+ req.query.file_name;
 res.download(fileName,function(err){
 if(!err)console.log('down')
 });
}

The file itself is written to the specified path in the immediate previous request to a different route. Now this works fine on my local..My question is, what would happen once I deploy it onto heroku? would my file upload and download still work, as I've read that heroku uses an ephimeral file system which allows writing only to a tmp directory.

If that's the case, could someone walk me through on how exactly to write to that directory through node...and also, would the file end up getting deleted before I could download it, since the upload and download are part of separate requests? Thanks.

asked Apr 11, 2017 at 3:08

1 Answer 1

4

Heroku's "ephemeral filestystem" is due to the containerisation of the app. When your app is built, an LXC container is sent to the runtimes and executed.

That means any file written on the filesystem will not be persisted accross redeploys, app restarts and multiple dynos. Even in the tmp folder.

You shouldn't store those files on disk, but on a dedicated file storage platform such as Amazon S3 or Google Cloud Storage.

See https://devcenter.heroku.com/articles/s3-upload-node

answered Apr 11, 2017 at 9:54
Sign up to request clarification or add additional context in comments.

2 Comments

Is it ok to upload and read a file, (e.g. a CSV), and then have the system delete it? I'm not worried about preserving the file - just taking the contents and making a POST to an api. Thank you.
Yes, that's OK. As long as you don't expect the file to still be in the filesystem after the duration of the request.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.