Correct way to start file upload (in PHP)?

A quick (and hopefully simple) question: I need to start downloading the PDF file generated by the PHP file. I can do it:

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

but should i do it differently? Maybe Javascript? The above works, but the window displays "Loading ..." before starting the download. I would like to inform users that something is happening.

Ideas?

Note. I already have a code that sends a file from the server. It works great. This question is how best to call this script from the client.

Some sites automatically download downloads. How do they do it?

The problem with the direct URL is that with PHP script errors, it will replace the contents of the existing page, which I don't want.

+7
javascript html download
source share
4 answers

EDIT

Yes javascript, something like:

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

If you really need to understand when loading starts, 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); } 
+13
source share

The solution you have to download is great. You might want to consider some visual user feedbacks, perhaps using javascript to show “Download, please wait for a message” on the current page when you click the link through the onclick handler. Or simply indicate that the download may take some time to start with a link. Since IE unloads the page, stopping any GIF animations, I prefer text directions for downloading files.

+3
source share

fake it with onclick event handler to show rotating gif

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

Auto-loading downloads typically use a meta tag inside a regular page:

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

In this example, the browser will be redirected 10 seconds before download.php.

+2
source share

All Articles