最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Correct way to trigger a file download (in PHP)? - Stack Overflow

programmeradmin5浏览0评论

Quick (and hopefully easy) question: I need to trigger a download of a PDF file that's generated by a PHP file. I can do this:

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

but should I be doing this another way? Javascript maybe? The above works but the window shows "Loading..." until the download starts. I'd like to provide some feedback to the user that something is happening.

Ideas?

Note: I already have the code that sends the file from the server. It works perfectly. This question is simply about how best to call that script from the client.

Some sites have downloads that automatically start. How do they do that?

The problem with a direct URL is that if the PHP script errors it'll replace th econtent of the existing page, which is not what I want.

Quick (and hopefully easy) question: I need to trigger a download of a PDF file that's generated by a PHP file. I can do this:

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

but should I be doing this another way? Javascript maybe? The above works but the window shows "Loading..." until the download starts. I'd like to provide some feedback to the user that something is happening.

Ideas?

Note: I already have the code that sends the file from the server. It works perfectly. This question is simply about how best to call that script from the client.

Some sites have downloads that automatically start. How do they do that?

The problem with a direct URL is that if the PHP script errors it'll replace th econtent of the existing page, which is not what I want.

Share Improve this question edited Dec 24, 2012 at 16:17 Zach Lysobey 15.7k21 gold badges100 silver badges151 bronze badges asked Jan 15, 2009 at 5:36 cletuscletus 625k169 gold badges918 silver badges945 bronze badges 3
  • 1 This question has nothing to do with PHP, and the way it's been titled, phrased and tagged misleadingly attracts visitors looking for something completely different. I'm writing this because that's the first result I got when looking for something which really is about PHP. I tried to edit but my edit was rejected :( So I'm kindly asking the original poster or someone else who's privileged enough to remove any references to PHP from this post. – Tom Commented Aug 23, 2012 at 13:12
  • That happened to me too Tom, edited. – Zach Lysobey Commented Dec 24, 2012 at 16:19
  • I actually found this useful because it came up first when I searched 'php server download file using javascript' – Boom Commented Apr 22, 2015 at 18:57
Add a comment  | 

4 Answers 4

Reset to default 13

EDIT

Yes javascript, something like:

<a href="download.php" onclick="this.innerHTML='Downloading..'; downloadPdf(this);">Download</a>

If you need to actually understand when the download is started you probably need to call an iframe and then use the "onload" event on it.. for example:

// javascript
function downloadPdf(el) {
    var iframe = document.createElement("iframe");
    iframe.src = "download.php";
    iframe.onload = function() {
        // iframe has finished loading, download has started
        el.innerHTML = "Download";
    }
    iframe.style.display = "none";
    document.body.appendChild(iframe);
}

The solution you have for download is fine. You may want to consider some visual feedback to the user, perhaps by using javascript to show a "Downloading, please wait message" on the current page when the link is clicked via an onclick handler. Or simply indicate that the download may take some time to start next to the link. Since IE will unload the page, stopping any GIF animations, I prefer text indications for file downloads.

fake it by using an onclick event handler to show a spinning gif

<a href="download.php" onclick="ShowDownloading();">Download</a>

Automatically starting downloads usually use a meta tag inside a normal page:

<META HTTP-EQUIV="REFRESH" CONTENT="10.0;URL=download.php">

This example will redirect the browser in 10 seconds to download.php.

发布评论

评论列表(0)

  1. 暂无评论