简体   繁体   English

如何通过单击按钮从服务器下载文件

[英]How to download a file from server with a button click

Let's say I have this file on my server: 假设我的服务器上有这个文件:

server/myFile.txt

How can I make the user download it with a button click? 如何通过单击按钮让用户下载它? Am I missing a simple solution? 我错过了一个简单的解决方案?

I am using jQuery & PHP if there is a solution for that. 如果有解决方案,我正在使用jQuery和PHP。

thanks, Alon 谢谢,Alon

I am not sure if I am misunderstanding your question otherwise it should be as simple as: 我不确定我是否误解了你的问题,否则它应该如此简单:

<a href="link/to/file">download file</a>

You shouldn't need to use js or php at all, Apache or whatever other webserver you are using should take care of handling the download all by itself. 你根本不需要使用js或php,Apache或你正在使用的任何其他网络服务器都应该自己处理下载。 Simply let the user download it straight off the server. 只需让用户直接从服务器下载即可。

If you want to force a "save-as" dialog box to open for a file type that your web server is configured to output as a readable document (and you don't want to change that configuration), you need to create a file that modifies the headers so that the browser creates such a dialog. 如果要强制“保存为”对话框打开文件类型,您的Web服务器配置为输出为可读文档(并且您不想更改该配置),则需要创建文件修改标题,以便浏览器创建这样的对话框。

See the very first example in the php documentation for readfile . 请参阅php文档中有关readfile的第一个示例。

You would then create a PHP file that could be accessed through a normal a href link, which would send the appropriate headers and output myFile.txt . 然后,您将创建一个可以通过普通a href链接访问的PHP文件, a href链接将发送相应的标头并输出myFile.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM