Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Download a text file from an HTML site

Posted on 2000-03-27
11
Medium Priority
?
165 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
[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
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 1

Accepted Solution

by:
lambda earned 400 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
Technology Partners: 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
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)

721 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