CJ7
CJ7

Reputation: 23285

save directly to file and get filename using WWW::Mechanize

I would normally save to a file using this:

$mech->save_content($mech->response->filename);

But due to some big files which cause "Out of memory" I have to use this instead:

$mech->get( $url, ":content_file"=>$tempfile );

How can I get the filename with the second method, or do I have to make one up?

I want the filename that would be returned in the response object: $mech->response->filename. I don't want to make up my own filename.

Upvotes: 2

Views: 177

Answers (1)

simbabque
simbabque

Reputation: 54373

The :content_file option is inherited from LWP::UserAgent and behaves in the same way. You don't know the file beforehand.

You could do a HEAD request to check the filename, and then do a GET request.

Alternatively, have a look at the lwp-download utility that ships with LWP::UserAgent. It provides exactly what you need. You could use it directly, or lift the stuff you want out of it. A WWW::Mechanize object can be dropped into this code and will behave exactly the same as the LWP::UserAgent one.

Upvotes: 2

Related Questions