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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21088376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:28:49  来源:igfitidea点击:

How to force fully download txt file on link?

htmldownloadanchor

提问by Dhaval

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>

回答by Efekan

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.

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

回答by PurkkaKoodari

You can use the Content-Dispositionheader.

您可以使用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>

回答by Adam Winnipass

You can do this

你可以这样做

<a href="data:text/plain;charset=UTF-8,test.txt" download>Click here</a>

<a href="data:text/plain;charset=UTF-8,test.txt" download>Click here</a>

Or in my case I needed something more dynamic

或者就我而言,我需要更动态的东西

var downloadFile = function(url){
  let a = document.createElement('a');
  a.href = 'data:text/plain;charset=UTF-8,' + '' + url;
  a.download = url.substr(url.lastIndexOf('/') + 1);
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
}

The main difference was appending

主要区别是附加

data:text/plain;charset=UTF-8,

数据:文本/普通;字符集=UTF-8,

to my text file url

到我的文本文件网址

downloadFile('http://my.txt');

下载文件(' http://my.txt');

回答by David

This will download your text file, and rename it :

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

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

回答by Sylas Seabrook

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 等语言将其作为下载发送。