CSV file download from button click through POST request

2,839 views
Skip to first unread message

pilatus

unread,
Apr 19, 2013, 7:16:54 PM4/19/13
to casp...@googlegroups.com
Hi.
 I want to download a CSV file, it is generated on a button click through a POST request. I researched to my best on casperJs and phantomJS forums and returned empty handed. It is too hard to believe that it is not supported. is there any nice trick though? pleas help

Thanks,

pilatus

unread,
Apr 19, 2013, 7:27:40 PM4/19/13
to casp...@googlegroups.com
Here are my response headers

TTP/1.1 200 OK Cache-Control: private Content-Type: text/html; charset=utf-8 Content-Encoding: gzip Vary: Accept-Encoding Server: Microsoft-IIS/7.5 Content-disposition: attachment;filename=ExportData.csv X-AspNet-Version: 2.0.50727 X-Powered-By: ASP.NET Date: Fri, 19 Apr 2013 23:26:40 GMT Content-Length: 65183

Leandro Boscariol

unread,
Apr 19, 2013, 7:30:32 PM4/19/13
to casp...@googlegroups.com
Have you tried CasperJS download method?

Cheers,
Leandro


--
CasperJS homepage & documentation: http://casperjs.org/
CasperJS @github: https://github.com/n1k0/casperjs
 
You received this message because you are subscribed to the Google Groups "casperjs" group.
Visit this group at http://groups.google.com/group/casperjs?hl=en.
---
You received this message because you are subscribed to the Google Groups "CasperJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to casperjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

pilatus

unread,
Apr 22, 2013, 5:54:58 AM4/22/13
to casp...@googlegroups.com
@Leandro There is no link generated to download the csv, the post request just returns the csv as an attachement. I wonder if casperJs download supports that

Leandro Boscariol

unread,
Apr 22, 2013, 8:55:54 AM4/22/13
to casp...@googlegroups.com
I understand.
I would suggest to ask on PhantomJS's mailing list, but they already have an old open issue for that (https://github.com/ariya/phantomjs/issues/10052).
I had the same issue, but I managed to find out a POST url address hidden inside the page to use the method mentioned before.
Sorry can't help you more on that :/

Leandro

Lalo Ortega

unread,
Aug 28, 2014, 2:18:50 PM8/28/14
to casp...@googlegroups.com
Hi!! @pilatus how are you? coder colleague :)
Did you find the solution to this issue?
I have the same problem, can you help me please?

an...@datorama.com

unread,
Nov 16, 2014, 10:06:27 AM11/16/14
to casp...@googlegroups.com
Hey!
I also have the exact same problem, and I really need a solution for this...
Anyone here knows about any changes..?

Thanks!

Victor Porton

unread,
Nov 16, 2014, 11:45:11 AM11/16/14
to casp...@googlegroups.com
// Something like this:
 
var ignoreResources = true; // set false before querying for a file
var downloadFilename;

function fileLoader(casper, response) {
  if(ignoreResources || response.stage!="start" || response.contentType!="text/csv") return; // substitute right content type
//  console.log('Response (#' + response.id + ', stage "' + response.stage + '"): ' + JSON.stringify(response));
  casper.download(response.url, "files/"+downloadFilename); // correct the folder
  ignoreResources = true;
}

var casper = require('casper').create({
    loadImages:  false,
    loadPlugins: false,
    onResourceReceived: fileLoader,
});
 
// ...
    ignoreResources = false;
    downloadFilename = fileInfo.name;
    // click() here
// ...
 
16.11.2014, 17:06, "an...@datorama.com" <an...@datorama.com>:
--
CasperJS homepage & documentation: http://casperjs.org/
CasperJS @github: https://github.com/n1k0/casperjs
 
You received this message because you are subscribed to the Google Groups "casperjs" group.
Visit this group at http://groups.google.com/group/casperjs?hl=en.
---
You received this message because you are subscribed to the Google Groups "CasperJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to casperjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
 
 
--
Victor Porton - http://portonvictor.org
 

an...@datorama.com

unread,
Nov 17, 2014, 10:34:07 AM11/17/14
to casp...@googlegroups.com
I'm doing the same thing, with the download method, but I get a file with error, probably due to that I need to add a cookie or something.
Bottom line I can't generate the same request twice since each request has special parameters that I can't figure out.
Also, the steps I do is to click on a button that triggers file download in the browser, and I use:

casper.on('resource.received', function (resource) {
  //my code
}

I can see that the file is downloaded from the web, but I can't reach it...
The question is why I can't access the file...

Thanks
Reply all
Reply to author
Forward
0 new messages