是否有办法强迫用户从href链接下载文件,而不是在浏览器窗口中打开它?

时间:2022-04-01 07:32:20

Basically I wrote a script that generates a xml file based on user input. After the file is generated a download link appears like so:

基本上,我编写了一个基于用户输入生成xml文件的脚本。文件生成后,下载链接如下:

<a href="path/to/file.xml">Download File</a>

But when clicked it opens the xml in the browser, I want it to start downloading when the link it clicked instead. Is there any way to achieve that?

但当点击时,它会在浏览器中打开xml,我希望它在点击链接时开始下载。有什么办法实现这一点吗?

5 个解决方案

#1


6  

Yeah, there is. It does require specifying some headers. Exactly how it works depends on what language you're using, but here's an example using php, taken off of php.net:

是的,有。它确实需要指定一些标题。它的具体工作方式取决于您使用的语言,但是这里有一个使用php的示例,摘自php。net:

<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');
?>

Basically, first we tell the client what type of file we're sending, then we tell the client that what we're sending is an attachment, and it's name, instead of it being a page to display, and then finally we print/read the file to the output.

基本上,首先我们告诉客户端我们要发送什么类型的文件,然后我们告诉客户端我们要发送的是一个附件,它是一个名称,而不是一个要显示的页面,然后我们打印/读取文件到输出。

Given that you're already using php to generate the xml file, I would suggest adding the header commands above to the code that generates the xml file, and see if that does the trick.

既然您已经在使用php生成xml文件,那么我建议将上面的头命令添加到生成xml文件的代码中,看看这是否有效。

#2


1  

If you happen to be using Apache for your web server, and you always want to force downloading of XML files, there is a more efficient way to do what @chigley suggested. Just add the following to a .htaccess file.

如果您碰巧在web服务器上使用Apache,并且您总是希望强制下载XML文件,那么有一种更有效的方法可以执行@chigley的建议。只需将以下内容添加到.htaccess文件。

<Files *.xml>
ForceType application/xml
Header set Content-Disposition attachment
</Files>

#3


0  

What happens when a browser sees a link is not dependent on the link, but rather on the target of the link. Your web server should send the appropriate header: Content-Disposition: attachment;filename="file.xml" to tell the browser that it should prompt to save the file instead of displaying it.

当浏览器看到一个链接时所发生的不是依赖于链接,而是依赖于链接的目标。您的web服务器应该发送适当的头:内容配置:附件;文件名=“file”。告诉浏览器它应该提示保存文件而不是显示它。

#4


0  

It depends on what the client computer does with XML files. If you doubleclick on a XML file, it will open in your browser probably.

这取决于客户端计算机处理XML文件的方式。如果在XML文件上双击,它可能会在浏览器中打开。

#5


0  

download.php:

download.php:

header('Content-Type: text/xml');
header('Content-Disposition: attachment; filename="file.xml"');
readfile('/path/to/file.xml');

HTML:

HTML:

<a href="download.php">Download</a>

#1


6  

Yeah, there is. It does require specifying some headers. Exactly how it works depends on what language you're using, but here's an example using php, taken off of php.net:

是的,有。它确实需要指定一些标题。它的具体工作方式取决于您使用的语言,但是这里有一个使用php的示例,摘自php。net:

<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');
?>

Basically, first we tell the client what type of file we're sending, then we tell the client that what we're sending is an attachment, and it's name, instead of it being a page to display, and then finally we print/read the file to the output.

基本上,首先我们告诉客户端我们要发送什么类型的文件,然后我们告诉客户端我们要发送的是一个附件,它是一个名称,而不是一个要显示的页面,然后我们打印/读取文件到输出。

Given that you're already using php to generate the xml file, I would suggest adding the header commands above to the code that generates the xml file, and see if that does the trick.

既然您已经在使用php生成xml文件,那么我建议将上面的头命令添加到生成xml文件的代码中,看看这是否有效。

#2


1  

If you happen to be using Apache for your web server, and you always want to force downloading of XML files, there is a more efficient way to do what @chigley suggested. Just add the following to a .htaccess file.

如果您碰巧在web服务器上使用Apache,并且您总是希望强制下载XML文件,那么有一种更有效的方法可以执行@chigley的建议。只需将以下内容添加到.htaccess文件。

<Files *.xml>
ForceType application/xml
Header set Content-Disposition attachment
</Files>

#3


0  

What happens when a browser sees a link is not dependent on the link, but rather on the target of the link. Your web server should send the appropriate header: Content-Disposition: attachment;filename="file.xml" to tell the browser that it should prompt to save the file instead of displaying it.

当浏览器看到一个链接时所发生的不是依赖于链接,而是依赖于链接的目标。您的web服务器应该发送适当的头:内容配置:附件;文件名=“file”。告诉浏览器它应该提示保存文件而不是显示它。

#4


0  

It depends on what the client computer does with XML files. If you doubleclick on a XML file, it will open in your browser probably.

这取决于客户端计算机处理XML文件的方式。如果在XML文件上双击,它可能会在浏览器中打开。

#5


0  

download.php:

download.php:

header('Content-Type: text/xml');
header('Content-Disposition: attachment; filename="file.xml"');
readfile('/path/to/file.xml');

HTML:

HTML:

<a href="download.php">Download</a>