Download File After Clicking Form

3,454 views
Skip to first unread message

foureight84

unread,
Jun 10, 2012, 9:46:50 PM6/10/12
to CasperJS
Hi Everyone,

At work we have a file repository website, which uses a proprietary
URL encryption. Let's say you have File A with the link
http://internal/files/429usfjsai145 when you paste this into your
browser, there is a login page. After filling in company credential, a
file save will appear. I've examined the form and it seems that
there's a javascript file that's handling the POST request and
credential validation, this same javascript also generates some
encoded text before POSTing along with the login data.

So far I've been able to login, but I don't know how to get CasperJS
to accept the response header with the file and save it to a local
directory.

Here's an example of the response header:

Cache-Control:private
Content-Disposition:attachment;filename="requested_file.zip"
Content-Length:2631615
Content-Type:application/x-zip-compressed
Date:Mon, 11 Jun 2012 01:20:43 GMT
Expires:Mon, 11 Jun 2012 01:19:42 GMT
Server:Microsoft-IIS/7.5
X-AspNet-Version:2.0.50727
X-Powered-By:ASP.NET

Any tip you can provide is greatly appreciated!!!

Nicolas Perriault

unread,
Jun 11, 2012, 1:50:22 AM6/11/12
to casp...@googlegroups.com
On Mon, Jun 11, 2012 at 3:46 AM, foureight84 <foure...@gmail.com> wrote:

> Content-Type:application/x-zip-compressed

PhantomJS can't deal with responses containing binary contents
http://code.google.com/p/phantomjs/issues/detail?id=52

I have no way in CasperJS to deal with this issue drectly, as I can't
even listen to the event you describe here.

So your best bet would be to submit the form using an Ajax POST
request, then retrieve the binary contents and encode them to base64
client-side, then base64decode it casperjs side, then save the binary
contents on your local filsystem.

Have a look at the machinery of Casper.download() to get inspiration if needed.

In all honesty, good luck.

++

--
Nicolas Perriault
https://nicolas.perriault.net/http://www.akei.com/
Skype: nperriault
Phone: +33 (0) 660 92 08 67

foureight84

unread,
Jun 18, 2012, 7:00:59 PM6/18/12
to casp...@googlegroups.com
Thanks Nicolas. I will give that a try. At this point in time this is the best tip I have to work off.

David Mc

unread,
Jun 25, 2012, 5:35:21 PM6/25/12
to casp...@googlegroups.com
I'm trying to do something similar (download a file that's in a response to a POST request), did you ever figure this out?


On Monday, June 18, 2012 4:00:59 PM UTC-7, foureight84 wrote:
Thanks Nicolas. I will give that a try. At this point in time this is the best tip I have to work off.

On Sunday, June 10, 2012 10:50:22 PM UTC-7, Nicolas Perriault wrote:

David Mc

unread,
Jun 26, 2012, 11:25:23 AM6/26/12
to casp...@googlegroups.com
Just to add more information for people who might find this thread in the future:

This issue is related to Issue 52 in phantomjs.

For the page we were looking at, we were able to fill out the POST form manually via the base64encode function something like
base64results = this.base64encode(postUrl, 'POST', postParams);
You can then use a base64 decoding function (Issue 52 above has some ideas) to access your data (in our case, a csv file) and do what you want from there

Nicolas Perriault

unread,
Jun 26, 2012, 11:47:08 AM6/26/12
to casp...@googlegroups.com
On Tue, Jun 26, 2012 at 5:25 PM, David Mc <dmcc...@phunware.com> wrote:

> For the page we were looking at, we were able to fill out the POST form
> manually via the base64encode function something like
> base64results = this.base64encode(postUrl, 'POST', postParams);
> You can then use a base64 decoding function (Issue 52 above has some ideas)
> to access your data (in our case, a csv file) and do what you want from
> there

Nice trick, thanks for sharing. I wonder if it works for binary files though…

Robert Jordan

unread,
Aug 31, 2012, 10:11:26 PM8/31/12
to casp...@googlegroups.com
It looks like someone has posted a solution to this here:


His example is this:

var page = require('webpage').create();

page.onUnsupportedContentReceived = function(data) {
   console.log('Got a download at url: ' + data.url);
   page.saveUnsupportedContent('some.file.path', data.id);
   phantom.exit();
}

page.open('http://some.pdf.url.com/some.pdf');

Can any clue a newbie such as myself how to wire this into the CaspeJS event system?  I tried:

casper.on('page.created', function(page){
  page.onUnsupportedContentReceived = function(data){
   casper.echo('Got a download at url: ' + data.url);
   page.saveUnsupportedContent('download.pdf', data.id);
  }
});

but I still just see:
[warning] [phantom] Loading resource failed with status=fail (HTTP 200):

when it should be downloading a pdf.

Thanks for any suggestions!
Robert

Sateesh Kavuri

unread,
Nov 24, 2015, 1:44:25 AM11/24/15
to CasperJS
I am looking for a solution to download a file on click of a button (response to a POST method). Is there a solution available for this?

Thank you

Robert Poor

unread,
Nov 24, 2015, 6:11:35 AM11/24/15
to CasperJS
I just posted a solution here:


for downloading a file (summary: construct your own form data and call casper.download(url, filename, 'POST', form_data)).  If you want it in-memory rather than saving it to a file, David Mc's approach using base64encode() should do the trick.

Reply all
Reply to author
Forward
0 new messages