Question

I have a rake task that will generate a particular CSV file. I would like to be able to download that CSV file that is going to be placed in /tmp.

My application is hosted in Heroku. How can I download that CSV file?

Was it helpful?

Solution 2

The /tmp directory on Heroku is exactly that – temporary. Even if you store the file in the /tmp file, it won't be persisted for long enough that any users will be able to access it. Instead, you should look into an integrated storage solution like Amazon AWS.

With that in place, your users should be able to access those CSV files directly from your storage host without needing to tie up any Heroku dynos/resources.

OTHER TIPS

If you just want to do a one off download, you could try using heroku exec. The exec command allows you to create an SSH connection to a dyno - https://devcenter.heroku.com/changelog-items/1112

First, figure out the file path. You can run bash, then do usual bash commands like ls:

heroku ps:exec -a <myapp> bash

Next, use cat to read the file, and pipe the output to a local file:

heroku ps:exec -a <myapp> cat tmp/myfile.csv > mylocal.csv

why is it necessary to place it in tmp folder? if you generate something it has to be important file not temporal one...

solution is easy, just setup your rake task in a way when your file will be saved into public directory (or subdirectory of public directory)

and then you can open/download your export.csv using

http://your-domain/[subdirectory-in-public-directory]/export.csv url

Files in the tmp directory are emptied everyday, tmp directory lives @:

/app/tmp

where app is the root directory

To download files from it you can read the file and convert it into a base 64 and send it back to the client as a data URL:

Server:

    let filePath = path.join(__dirname, '..', '..', 'tmp', fileName);

fs.readFile(filePath, {encoding: 'base64'}, function (err, data) {
                        if (!err) {
                            let returnData = `data:${mimeType};base64,` + data;
                            res.json({fileName: fileName, displayName: displayName, base64: returnData})
                        } else {
                            console.log(err);
                        }
                    });

Client side:

 function b64toBlob(dataURI) {

    var byteString = atob(dataURI.split(',')[1]);
    var ab = new ArrayBuffer(byteString.length);
    var ia = new Uint8Array(ab);

    for (var i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
    }
    return new Blob([ab], { type: 'image/jpeg' });
}


        var blob = b64toBlob(res.data.base64);

        var blobUrl = URL.createObjectURL(blob);
        var link = document.createElement("a"); // Or maybe get it from the current document
        link.href = blobUrl;
        link.download = res.data.displayName;
        document.body.appendChild(link)
        link.click()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top