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. In a normal browser like firefox, a browser download dialog window appears after the post request. How to handle this case in PhantomJS
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
I've found a way to do this using casperjs (it should work with phantomjs alone if you implement the download function using XMLHttpRequest, but i've not tried).
I'll leave you the working example, that tries to download the mos recent PDF from this page. When you click the download link, some javascript code is triggered that generates some hidden input fields that are then POSTed.
What we do is replace the form's onsubmit function so that it cancels the submission, and get the form destination (action) and all its fields. We use this information later to do the actual download.
Note: you have to run it with --ignore-ssl-errors, as the CA they use isn't in your browser default CA list.
You can listen to the
page.resource.received
event anddownload()
the file when received:@julianjm aproach is almost the solution, but in my case i did not have the correct form name to replace the form submission.
So i found another solution using phantomjs beta:
There is a beta version of phantomjs 2.0 that includes an event handler that solves this issue.
It is still a beta version, so there is no debugging.
So i have developed the clicks and the page treatments on the release version and then changed the phantom version to make download work.
Download: Phantom 2.0 BETA
Download the exe, rename the release version of phantom.exe to phantom.bkp.exe and insert this 2.0 version on the place. Then, in casperjs you will need to add some lines at the beggining of casperjs/bin/bootstrap.js
also comment the version check (same file):
Remember, this is a tweak!!.
So this lines on bootstrap will cause problems if you want to run phantom release version or slimerjs.
So DEVELOP ON RELEASE VERSION, than tweak to this version to be able to download. If you need to debug, you will have to remove the lines of bootstrap.js
I have to deal with a site written with some kind of ASP.Net framework which sends a remarkable amount of POST data at each request (some 100 Kb of data, of which about 95 never seem to change between requests - viewport state related apparently).
However, no method I could find worked for me. I've looked into intercepting XHR, I've even found someone who is tackling the very same framework (at least judging from the selectors) but with a simpler case, inspired by this very question. I found out that back in the day this couldn't be done with PhantomJS.
My problem is that a click on a button starts a chain of AJAX requests culminating with the sending of this enormous POST form, to which finally the server replies with a "Content-Disposition: attachment".
In the end, I found this approach which works for me, even if it is network-inefficient:
The download works flawlessly, even if I can see that the file gets requested (and sent) twice.
(Next, I'll probably modify the script to try invoking
request.abort()
from inside theresource.requested
listener, set a semaphore and invoke again the downloader - I won't be able to get the attachment filename, but that matters little to me).