Solved

Download a text file from an HTML site

Posted on 2000-03-27
11
160 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Bootstrap input box width 2 34
HTML File in SharePoint 2013 Library 4 47
.php tree directory? 5 56
jquery - show / hide div depending on day and time 19 30
This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

920 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

16 Experts available now in Live!

Get 1:1 Help Now