如何强制完全下载链接上的txt文件?

时间:2022-10-22 14:44:37

I have one simple text file and I want to download that file on any anchor tag link.

我有一个简单的文本文件,我想在任何锚标记链接上下载该文件。

But when I click on that link txt file shown me but not downloaded.

但是,当我点击链接txt文件显示我但没有下载。

I have try this code

我试过这段代码

<html>
    <head>
        <title>File</title>
    </head>
    <body>
        <a href="test.txt">Click here</a>
    </body>
</html>

4 个解决方案

#1


44  

Download file when clicking on the link (instead of navigating to the file):

单击链接时下载文件(而不是导航到文件):

<a href="test.txt" download>Click here</a>

Download file and rename it to mytextdocument.txt:

下载文件并将其重命名为mytextdocument.txt:

<a href="test.txt" download="mytextdocument">Click here</a>

The download attribute specifies that the target will be downloaded when a user clicks on the hyperlink.

download属性指定当用户单击超链接时将下载目标。

This attribute is only used if the href attribute is set.

仅在设置href属性时才使用此属性。

The value of the attribute will be the name of the downloaded file. There are no restrictions on allowed values, and the browser will automatically detect the correct file extension and add it to the file (.img, .pdf, .txt, .html, etc.).

属性的值将是下载文件的名称。允许值没有限制,浏览器将自动检测正确的文件扩展名并将其添加到文件(.img,.pdf,.txt,.html等)。

If the value is omitted, the original filename is used.

如果省略该值,则使用原始文件名。

#2


4  

You can use the Content-Disposition header.

您可以使用Content-Disposition标头。

You can do it with PHP or with .htaccess.

您可以使用PHP或.htaccess来完成。

PHP:

PHP:

<?php
header("Content-Disposition: attachment");
header("Content-Type: text/plain"); // optional
readfile("yourfile.txt");
?>

And then you can either use the PHP's URL or redirect the TXT's one to it. If you want to use the PHP's URL but want to save the file with the original name you can swap this line in there:

然后你可以使用PHP的URL或将TXT的URL重定向到它。如果您想使用PHP的URL但想要使用原始名称保存文件,您可以在那里交换此行:

header("Content-Disposition: attachment; filename=yourfile.txt");

.htaccess:

的.htaccess:

<Files yourfile.txt>
    Header set Content-Disposition attachment
</Files>

#3


0  

Text files are displayed in the browser when the content-type is sent as text. You'd have to change the server to send it with a different content-type or use a language such as PHP to send it as a download.

当内容类型作为文本发送时,文本文件将显示在浏览器中。您必须更改服务器以使用不同的内容类型发送它,或使用PHP等语言将其作为下载发送。

#4


0  

This will download your text file, and rename it :

这将下载您的文本文件,并重命名为:

 <a href="http://www.example.com/myfile.txt" download="My Text File">click here</a>  

#1


44  

Download file when clicking on the link (instead of navigating to the file):

单击链接时下载文件(而不是导航到文件):

<a href="test.txt" download>Click here</a>

Download file and rename it to mytextdocument.txt:

下载文件并将其重命名为mytextdocument.txt:

<a href="test.txt" download="mytextdocument">Click here</a>

The download attribute specifies that the target will be downloaded when a user clicks on the hyperlink.

download属性指定当用户单击超链接时将下载目标。

This attribute is only used if the href attribute is set.

仅在设置href属性时才使用此属性。

The value of the attribute will be the name of the downloaded file. There are no restrictions on allowed values, and the browser will automatically detect the correct file extension and add it to the file (.img, .pdf, .txt, .html, etc.).

属性的值将是下载文件的名称。允许值没有限制,浏览器将自动检测正确的文件扩展名并将其添加到文件(.img,.pdf,.txt,.html等)。

If the value is omitted, the original filename is used.

如果省略该值,则使用原始文件名。

#2


4  

You can use the Content-Disposition header.

您可以使用Content-Disposition标头。

You can do it with PHP or with .htaccess.

您可以使用PHP或.htaccess来完成。

PHP:

PHP:

<?php
header("Content-Disposition: attachment");
header("Content-Type: text/plain"); // optional
readfile("yourfile.txt");
?>

And then you can either use the PHP's URL or redirect the TXT's one to it. If you want to use the PHP's URL but want to save the file with the original name you can swap this line in there:

然后你可以使用PHP的URL或将TXT的URL重定向到它。如果您想使用PHP的URL但想要使用原始名称保存文件,您可以在那里交换此行:

header("Content-Disposition: attachment; filename=yourfile.txt");

.htaccess:

的.htaccess:

<Files yourfile.txt>
    Header set Content-Disposition attachment
</Files>

#3


0  

Text files are displayed in the browser when the content-type is sent as text. You'd have to change the server to send it with a different content-type or use a language such as PHP to send it as a download.

当内容类型作为文本发送时,文本文件将显示在浏览器中。您必须更改服务器以使用不同的内容类型发送它,或使用PHP等语言将其作为下载发送。

#4


0  

This will download your text file, and rename it :

这将下载您的文本文件,并重命名为:

 <a href="http://www.example.com/myfile.txt" download="My Text File">click here</a>