简体   繁体   中英

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.

thanks, 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. 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.

See the very first example in the php documentation for 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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