Link to home
Start Free TrialLog in
Avatar of rkrenek
rkrenek

asked on

Returning file problem.

Hello,
I have a CGI that is returning a file (byte stream) I'm setting the Content Type as application/octet-stream. My question/problem is I want to tell the browser the initial file name to save the file as. The url to the program is:
"http://bla.bla.bla/example/GetFile?zipfile=rich.zip&file=TEST1%2FTEST2%2FTEST.ZIP" The file it trys to initaly save-as is GetFile I would like it to be TEST.ZIP. Any pointers would be great.
rkrenek
Avatar of thoellri
thoellri

rkrenek,

make sure that your default filename appears as last component in the url as in ..../TEST.ZIP. So your URL could actually be: http://bla.bla.bla/example/GetFile/TEST.ZIP?zipfile=rich.zip where TEST.ZIP would be part of path_info().

Here is some code (using perl and CGI.pm) which may help you:

What you need is perl and CGI.pm. You seem to have perl, otherwise you would not have posted to the perl-group. Here is the code which you can call download.pl if you want to:

#!/usr/local/bin/perl
use CGI qw/:standard/;
use HTTP::Date qw(time2str);
$|++;
# This is the file which will be sent to the user
my $zip="/user/thoellri/test.zip";
# This is the default filename the browser will select for the file
my $zipname="TheName.zip";
my $cgi=new CGI;
my ($read, $buf, $t);

if (!$cgi->path_info()) {
   # if the script doesn't have path_info() attached, redirect to ourself
   # this time with path_info() attached
   print $cgi->redirect($cgi->script_name()."/".$zipname);
} else {
   # We got path_info() send the data.
   $t=time2str;
   print $cgi->header(-nph => 1, -date => $t,
             "Last-modified" => $t, -pragma => "no-cache",
             -expires => 'now', -type => "application/x-zip" );
   open($zip,"<$zip");
   while($read=read($zip, $buf, 1024)) {
       print $buf;
   }
   close($zip);
}

Hope this helps
  Tobias


Avatar of rkrenek

ASKER

thoellri,
Unfortunatlly I was trying to get away from doing it that way. The main reason I need to pass 2 variables to my program. First the "zipfile" which is a zipped file, second the "file" which is the file within the zipped file that I want returned. GetFile is the actual program being run so I didn't think I could add a path after it before the "?". I could instead of doing "ttp://bla.bla.bla/example/GetFile?zipfile=rich.zip&file=TEST1%2FTEST2%2FTEST.ZIP" I could do "ttp://bla.bla.bla/example/GetFile?zipfile=rich.zip&file=TEST1/TEST2/TEST.ZIP" but that had problems when sending it by a HREF. Is there anything within my program that I can sent to force it to use a file name. ie "application/octet-stream;some magic here" or somethng in the header?
rkrenek
rkrenek,

unfortunately I know of no other way to do using MIME-magic. My experience (and look at the Mozilla source) tells me that the browser determines the default filename by looking at the last component of the URL.

Yes , you can add a path after GetFile as in:
  http://bla.bla.bla/GetFile/test.zip?zipfile=rich.zip&file=test1%2Ftest2%2ftest.zip 
Check the PATH_INFO environment when your script gets called and you'll see it in there.

Give it a try.

  Tobias

generate a page with your getfile.exe that contains
<script>
document.location.href="http://wherevere.com/test.zip";
</script>

If you want to be really cute you can even do this in a new window using window.open, this is what I do.
Avatar of rkrenek

ASKER

thoellri,
Thanks! Your last comment worked and will work perfectly for what I need to do. If you could istead of posting a comment post an answer here so I can give you the points for the answer. Or if you know tell me how to give the points to you.

Thanks a million,
rkrenek
rkrenek,

good to hear that you solved the problem. Unfortunately my friend jhurst posted an answer. You need to reject his answer first, before I can answer :-0

  Tobias
ASKER CERTIFIED SOLUTION
Avatar of thoellri
thoellri

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial