Solved

Download a text file from an HTML site

Posted on 2000-03-27
11
161 Views
Last Modified: 2010-04-09
Hi,

when you want to offer a download to the visitors of your site, you can use
<a href="filename.zip">Filename</a>
which works properly. But when I try to do the same thing with a text file the browser opens the file in the window. How can I get a text file downloaded, too?

Regards, Hamilton.
0
Comment
Question by:Hamilton
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 1

Accepted Solution

by:
lambda earned 100 total points
ID: 2660298
Try this::

<a href="/cgibin/dnload.cgi">Filename</a>


In dnload.cgi you'll have these lines...


$target_file = "/usr/files/testfile.txt"; #entire path to the file to be downloaded.
$file_name = "testfile.txt"; # name of the file to be downloaded.
   
    print "Content-Type: application/x-unknown\n";
    print "Content-Disposition: attachment; filename=$file_name\n\n";
   
    print read_file($target_file);


sub read_file {
    local($fname) = @_;
    local($content);

    open(FILE, "<$fname") || return '';

    while(<FILE>)
    {
        $content .= $_;
    }
    close(FILE);

    $content;
}


0
 
LVL 19

Expert Comment

by:daveamour
ID: 2660414
When a browser gets a file, if it has a file exstension it knows how to deal with such as htm, html, gif, txt etc it will process that file in the browser.

If it gets a file that it does not recognise the file exstenion of then it will give you the option to save the file to disk.

So your problem is simply that the browser is set to process and display text files (txt exstenion).

Therefore all you need to do is give the file a different exstenion such as "Filename.text" for example

Dave
0
 
LVL 8

Expert Comment

by:jbirk
ID: 2660573
You could also zip it up.  That's a good way of forcing it to be downloaded.  Most people who browse have some kind of unzipping software.

-Josh
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 1

Expert Comment

by:mafweb
ID: 2661542
another (simple) way to solve this would be to tell your users to hold down the shift key while clicking on the link. should work at least for ie & ns

0
 
LVL 11

Expert Comment

by:mouatts
ID: 2665475
daveamour is partly correct. The web server sends out a mime type which it determines from the file extension. The browser then displays etc the incoming data based on the mime-type not the actual extension. (having said that IE does look at the extension, wrongly, when it doesn't recognise the mime-type and will process some files based on this).

So you can do as suggested and use and extension like .text, however once saved to disk the person will not be able to double click unless they have this assocaite set.

Alternatively if you configure the server so that it sends a different mime-type for .txt then the browser can download and save and the double click functionality on the client machine will still operate.

application/octet is the obvious one that comes to mind to substitute with.

HTH
steve
0
 
LVL 1

Expert Comment

by:lambda
ID: 2669956
In the Perl code I have given above dnload.cgi, that's what happens...

The Content-type is set as an unknown value, application/x-unknown.

So the browser brings up the file download dialog box.
0
 

Author Comment

by:Hamilton
ID: 2670033
Hi lambda,

I've used your code in my site and it works very well. Thanx! I can't take a zip file because the text file is created dynamically by a Perl script.
Do you want to provide an answer to get the points?

Regards, Hamilton.
0
 

Author Comment

by:Hamilton
ID: 2670047
Hi lambda,

I've used your code in my site and it works very well. Thanx! I can't take a zip file because the text file is created dynamically by a Perl script.
Do you want to provide an answer to get the points?

Regards, Hamilton.
0
 
LVL 8

Expert Comment

by:jbirk
ID: 2671100
You can accept his comment as an answer.  You don't have to wait for him to post an answer.  There should be a button by each comment which says "Accept comment as answer" or something like that.

-Josh
0
 
LVL 1

Expert Comment

by:lambda
ID: 2671981
Thanks, Hamilton.
Glad to know ur problem is solved.
0
 

Author Comment

by:Hamilton
ID: 2672681
Hi jbirk, hi lambda,

i haven't noticed the button on the right although it's really not to be overseen. ;-) It is long ago that I was here in E-E, and this feature is somewhat new.
I've just seen my comment had been posted twice, because I twice tried to send my message, but the server did not respond and so I decided to try later on.

Regards, Hamilton.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Someone recently asked me about how to display a progress indicator on a page while an iframe is loading. And I remember when I first came across this myself. It was a bit tricky to get my head around, but really, it's very simple. The most impor…
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

786 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