Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

write to cookie then download file problem

Status
Not open for further replies.

GoSooJJ

Programmer
Feb 24, 2001
76
0
0
US
Hi all,

I'm trying to increment the number of download cookie before download file action happens(or after) but forcing download is not working anymore. If I run the code separately then it works fine. I think "print cookie" is causing the download file here. Instead to popup the download window, it just write file content(all junk characters) to browser.
Can anyone done this before or have any idea how I can write cookie and force download?

See below my code

$cookie = $cgi->cookie (
-name => 'num_down',
-value => $new_numdl,
-expires => '+1y',
-domain => '.mydomain.com'
);
print $cgi->header(-cookie=>$cookie);

open(DLFILE, "<$filepath/$filename") || Error('open', 'file');
@fileholder = <DLFILE>;
close (DLFILE) || Error ('close', 'file');

if ($ENV{'HTTP_USER_AGENT'} =~ /MSIE/) {
print "Content-Type: application/x-download\n";
} else {
print "Content-Type: application/octet-stream\n";
print "Cache-Control: no-cache\n";
}
print "Content-Disposition: attachment; filename=$x_filename\n\n";
print @fileholder
 
Hi

Just based on my observations, seems that only HTML document could have cookies, such HTTP headers are ignored if the [tt]Content-type[/tt] is not [tt]text/html[/tt].

You can try to set the cookie within a redirection and to implement the download in a second step. But possible to not work neighter this.

Anyway, why do you want to use that cookie ? Would not be easier and better to use some session variable ?

Feherke.
 
Thanks for quick reply feherke ^^.
In fact, I removed cookie part and used database to update the increment of number of downloads. This worked fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top