user752637
user752637

Reputation: 51

Perl / CGI: redirect to a different page after downloading a file

this is quite a newbie question and I've searched on this topic for a while, but nothing I've found seems to work as described. I have this piece of code, for providing a file download to the user, which works perfectly:

open(DOC, "<$file_name") or die "$!";
@textFile = <DOC>;  
close DOC;

print "Content-Type:application/x-download\n";  
print "Content-Disposition:attachment;filename=" . $basename . "\n\n";   
print @textFile;

My problem is that after the file-download has started, I would like to redirect the user to a different page. The script above is actually being submitted from a form by another script where I have:

<form action="/cgi-bin/download.pl">   
    <p> some msg </p>  
    <p><input type="submit" value="Download" name="Download"></p>            
    </form>

I've tried putting some javascript statements in the input-Tag like:

onclick="javascript:window.document.location.href=\'http://www.mynewpage.com'

as well as printing at the end (of download.pl) something like:

print "Location: http://www.mynewpage.com";

It doesn't work.

If someone could give me a hint, I'd really appreciate that!

Thanks in advance!

Alex

Upvotes: 2

Views: 1564

Answers (2)

Dave
Dave

Reputation: 14178

You cannot do this with HTTP (see ADW's resposne).

What most sites do is to redirect you to a page which has some javascript which then starts the download. It also provides a link in case that doesn't work.

For example: http://sourceforge.net/projects/sevenzip/files/7-Zip/9.22/7z922.exe/download

See this SO question about the javascript: starting file download with JavaScript

Upvotes: 1

ADW
ADW

Reputation: 4080

You can't do it using straight http/html.

The response to the query is a file, you're not allowed to send an additional response (i.e. a redirect) as well.

As Dave says, normally it's done with JavaScript.

Upvotes: 0

Related Questions