how to create a link that download...

batman1

New Member
Messages
92
Reaction score
0
Points
0
Hey I want ot download a text file but with the code below it opens the text file in a new window.

Code:
<a href="textfile.txt">download here</a>

Whats the code to make that when i click the link the file can be downloaded onto my computer.

Thanks:biggrin:
 
Last edited:

Livewire

Abuse Compliance Officer
Staff member
Messages
18,169
Reaction score
216
Points
63
That -is- the code to download it to pc - your systems browser is set to a different action by default however, so it's not having the intended effect.

In short the code is right, the browser's doing it wrong - not really a way to code it so it forces it to download that I know of, other than right-click, save-target-as :)
 

mattura

Member
Messages
570
Reaction score
2
Points
18
Once it opens in a new window, click file...save as. You have already downloaded it! What browser are you using?
 

sourfacedcyclop

New Member
Messages
221
Reaction score
0
Points
0
If you add it to a zip folder and upload it to the server and then link to the folder it should download.
 

zen-r

Active Member
Messages
1,937
Reaction score
3
Points
38
If you add it to a zip folder and upload it to the server and then link to the folder it should download.

I was about to say something similar myself!

Uploading it as a zip file will also make it smaller & save space / download time (though of course, it adds some hassle for the user having to unzip it again).
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
I guess it depends on the size of the text file. If you're zipping a 10kb text file (like most of them are :biggrin:) you really gain nothing by zipping it. Unless you anticipate your users are on 14.4kbs dialup. The .zip extension just guarantees that your browser won't try to open it. But, you can do the same thing with the link I posted above and use any file extension ;)
 

daman371

New Member
Messages
130
Reaction score
0
Points
0
You have send the header to download the file. It involves a little PHP or another web language. PHP is the one I know though. Put this code in a file called text.php or something. Of course you have to change the Content-Length parameter.

Code:
<?php
header('Content-Type: text/plain');
header('Content-Length: 1234');
header('Content-Disposition: attachment;filename="textfile.txt"');
$fp=fopen('textfile.txt','r');
fpassthru($fp);
fclose($fp);
?>
 

batman1

New Member
Messages
92
Reaction score
0
Points
0
You have send the header to download the file. It involves a little PHP or another web language. PHP is the one I know though. Put this code in a file called text.php or something. Of course you have to change the Content-Length parameter.

Code:
<?php
header('Content-Type: text/plain');
header('Content-Length: 1234');
header('Content-Disposition: attachment;filename="textfile.txt"');
$fp=fopen('textfile.txt','r');
fpassthru($fp);
fclose($fp);
?>

thanks for your reply I will try this.
 

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
I understand the text file problem, but why then do certain images enforce a download rather that just viewing them? That can get really irritating when image searching.
 

iwaproductions

New Member
Messages
17
Reaction score
0
Points
0
It's all in the syntax...

A .txt file is often simply shown on most Internet browsers, because basically it's just an html file without coding. I would suggest trying to put the text file into a .zip or .rar archive first. Then, usually, most browsers will pick up that it's something to download. Also, in the question about some images files doing the download syntax. Sometimes (and it's a bit quirky to me) The browser doesn't recognize that an image is viewable as such and tries to make you download the image first. Other images like .DDS format, used in some games, only become viewable after some drivers are installed in Explorer.

But anyway, I'm rambling. I hope that helps out some.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
I understand the text file problem, but why then do certain images enforce a download rather that just viewing them? That can get really irritating when image searching.
Usually it's because the server sends a MIME type for the image that the browser can't handle, so the browser prompts to save the file. You usually can't get the browser to display such media itself, as the MIME types the browser (and plugins) can handle are hard coded.
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
Concerning the problem of displaying the text file instead of prompting for download, there are many, many, many alternatives on google. Most of these are extremely easy to use, mod, customize. I've myself found one, extremely simple one, that is, and rewrote it to better fit my needs. Send me a PM if you'd like me to help you create an download system for your website.
 
Top