Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Download a text file from an HTML site

Posted on 2000-03-27
11
Medium Priority
?
166 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
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…

879 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