How to download a csv file using PhantomJS

I found a solution for PhantomJS. Reading through this discussion I found a jsfiddle which downloads a url via jQuery’s ajax method and encodes the file as base64.

The file I wanted to download was plain text (CSV) so I have removed the encoding functions. My target page also already had jQuery included so I didn’t need to inject jQuery into the target page.

My code assumes you have already opened the page you want to download the file from using PhantomJS, and that page has jQuery in it. In my case I had to first login to the site in order to get the download link.

var fs = require('fs');

var page=this;

var result = page.evaluate(function() {

    var out;
        'async' : false,
        'url' : 'fullurltodownload.csv',
        'success' : function(data, status, xhr) {
            out = data;
    return out;


fs.write('mydownloadedfile.csv', result);

Leave a Comment