Solved

forcing file download, not file view

Posted on 2000-05-01
10
136 Views
Last Modified: 2010-03-05
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
Comment
Question by:ShaunMarion
  • 5
  • 4
10 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 2767059
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
 

Author Comment

by:ShaunMarion
ID: 2768986
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
 

Author Comment

by:ShaunMarion
ID: 2769090
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
 
LVL 16

Expert Comment

by:maneshr
ID: 2769357
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
 

Author Comment

by:ShaunMarion
ID: 2769514
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:ShaunMarion
ID: 2769644
I don't know if this matters or not, but I am running this on a windows nt platform, not unix
0
 
LVL 16

Accepted Solution

by:
maneshr earned 100 total points
ID: 2769873
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
 

Author Comment

by:ShaunMarion
ID: 2770101
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
 
LVL 16

Expert Comment

by:maneshr
ID: 2770144
sorry i could not provide you the complete solution though .:-(
0
 

Expert Comment

by:tambde
ID: 4803222
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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now