Solved

forcing file download, not file view

Posted on 2000-05-01
10
142 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
[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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
transpose into pipe delemited 8 86
perl CPAN issue 3 131
Perl Script - Remove column of data based on column value 2 59
Replace  text in a file 2 134
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

752 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