Solved

Download a text file from an HTML site

Posted on 2000-03-27
11
159 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

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

13 Experts available now in Live!

Get 1:1 Help Now