Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Returning file problem.

Posted on 1999-07-08
7
Medium Priority
?
125 Views
Last Modified: 2013-12-25
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
0
Comment
Question by:rkrenek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 5

Expert Comment

by:thoellri
ID: 1834946
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


0
 

Author Comment

by:rkrenek
ID: 1834947
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
0
 
LVL 5

Expert Comment

by:thoellri
ID: 1834948
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

0
A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

 
LVL 8

Expert Comment

by:jhurst
ID: 1834949
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.
0
 

Author Comment

by:rkrenek
ID: 1834950
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
0
 
LVL 5

Expert Comment

by:thoellri
ID: 1834951
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
0
 
LVL 5

Accepted Solution

by:
thoellri earned 400 total points
ID: 1834952
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.

Thanks
  Tobias


0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question