Jump to content

How do they do it? A 'view' and 'download' link!


nloding

Recommended Posts

GMail is the best example I know of; a document, say a PDF file, is listed with a 'view' and a 'download' link immediately after.  When you click 'view', it opens as you would expect (in your browser, for instance).  If you click 'download', then the file download prompt is automagically brought up.

 

How?

 

I have a site I'm working on with an archived documents section.  I want this option available.  How?  HOW?  I've tried Googling it, but if you put the word 'download' in your search, you don't get anywhere.

SOLVED!  I found it, here's the code for those who are interested:

 

Markup:

<a href="#" onclick="downloadFile('20')">download</a>

<script type="text/javascript">
function downloadFile(id) {
var url = 'http://mywebsite.com/downloadfile.php?id=" + id;
var tempWindow = window.open(url);
}
</script>

 

Server-side (downloadfile.php)

<?php
$id = $_GET['id'];

//  however you find your filename and path, do it here, i used a mysql query

$filename = str_replace(" ", "_", $result['filename']);
$path = "/path/to/" . $result['filename'];

Header("Content-type: application/octet-stream");
Header("Content-length: " . filesize($path));
Header("Content-Disposition: attachment; filename=" . $filename);

$fp = fopen($path, 'rb');
fpassthru($fp);
fclose($fp);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.