Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 147
  • Last Modified:

forcing file download, not file view

I have writtin a simple PERL script to view the contents of a directory and then write a web page with links to the different files in that directory....that is all fine and dandy.  However, there are a few MS Word and Power Point files there that when the user clicks on the link, it tries to view the file in the browser instead of opening the save file dialog box.  I know I can have the users right click and go to save target as, however this is not what I want.  Is there any way to have the user click on the link and automatically open the save file dialog box with out automatically viewing the file in the browser?
0
ShaunMarion
Asked:
ShaunMarion
  • 5
  • 4
1 Solution
 
maneshrCommented:
yes that can be done.

instead of showing a link to the file directly, you should point to a CGI script. Now you will pass the name of this file to the CGI script.
Eg.
<a href="/cgi-bin/show.pl?f=sales.ppt">Sales.ppt<a>

All that the CGI script would do is open the file at the specified location (in binary mode - binmode) and print the contents of the file to the screen.
Also before you start printing the content you would have to send a unknown MIME header so that the "Save File" dialog box appears.

here is a very basic perl script that will do the above for you.

=================show.pl
#!/usr/local/bin/perl

use CGI;

$query=new CGI;

$file=$query->param('f');

$full_path="/www/temp/".$f;

print "Content-type: something/idontknow\n\n";

open(F,$full_path) || die $!;
binmode F;
while(<F>){print $_;}
close(F);

0
 
ShaunMarionAuthor Commented:
One last thing, that worked...sort of.  When I request the file, and the save file dialog box appears, it doesn't have the name of the file, it has the "show.pl" file there.  However, it is downloading the right file, because if I download it, and then rename it to the file I requested, it opens up like it should.
0
 
ShaunMarionAuthor Commented:
One last thing, that worked...sort of.  When I request the file, and the save file dialog box appears, it doesn't have the name of the file, it has the "show.pl" file there.  However, it is downloading the right file, because if I download it, and then rename it to the file I requested, it opens up like it should.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
maneshrCommented:
that is because of the way you are invoking the URL.

so if your URL is

http://www.xyz.com/cgi-bin/show.pl

& you go for the download, the name of the downloaded file will be whatever is present after the last / (which is show.pl in this case)

you can work around this by changing the href as follows.


<a href="/cgi-bin/show.pl/sales.ppt">Sales.ppt<a>

Also the PERL script will have to be modified as follows.

=======show.pl
#!/usr/local/bin/perl

use CGI;

$query=new CGI;

$file=$ENV{'PATH_INFO'};

$full_path="/www/temp".$file;

print "Content-type: something/idontknow\n\n";

open(F,$full_path) || die $!;
binmode F;
while(<F>){print $_;}
close(F);
0
 
ShaunMarionAuthor Commented:
I must be doing somethin wrong....
here is my href

DMS/show.pl/test.doc

I have the perl file like you said

When I call the href, it gives me error 404, file not found.  However, if I change the link to say

DMS/show.pl?test.doc

It runs, so I know the link is pointing to the right file, it just goes crazy after I put the "/" in there.  I can't see if the file is even running because it acts like it is looking for the directory "show.pl" and then a file called test.doc with in that directory.  Am I right?  or am I coding something wrong...??

0
 
ShaunMarionAuthor Commented:
I don't know if this matters or not, but I am running this on a windows nt platform, not unix
0
 
maneshrCommented:
ouch!! on NT are you using IIS?

if yes, then there is a problem.

IIS does not support adding anything after the name of the CGI script. So you cannot use /cgi-bin/show.pl/test.doc.

However you can do this in UNIX.

i have faced this problem myself in the past.

i dont know of any quick solution for this. :-(
0
 
ShaunMarionAuthor Commented:
Well, you got me headed in the right direction at least....a lot farther than I would have gotten with out you, so you can have the points....thanks!
0
 
maneshrCommented:
sorry i could not provide you the complete solution though .:-(
0
 
tambdeCommented:
Hi,
   It is the same problem i faced but the solution to this problem is very easy one u have to just set the http header
step 1 :> open file in binary mode
step 2 : > take entire content of file in a variable
step 3 : > set content type to octet-steam(so that out put which u r pronting will be saved on client without asking any question)
print qq(Content-type: application/octet-stream\n);
step 4 : > set Content-disposition to the name of the file (so that all the content which is being saved on the client machine will be saved as the file of that name)
print qq(Content-disposition: filename="$NAME_OF_THE_FILE"\n\n);
step 5 : > Print the content of that variable.

Note : u can provide name of the file as the query parameter.
any more queries welcomed.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now