Jim from Oakland Posted October 4, 2007 Share Posted October 4, 2007 I THINK this is the right place to ask... I have a file listing that serves up LINKS to files in a directory. I would like to give users the option to "save" the file to her/his computer. Here's what I want: In IE if I right click on the link I get the "Save (target) As" dialog box. How do I do that that with a button? Specifically what might js code look like? pseudo code <script "language=javascript"> function saveAsDialog(sFileName) { fileSaveAs("application/text", sFileName); } </script> Jim Quote Link to comment Share on other sites More sharing options...
php_tom Posted October 5, 2007 Share Posted October 5, 2007 I'm pretty sure that's not possible in JS. But you can do it with PHP: <input type='button' value='Save!' onclick='document.location.href=download.php' /> Then in download.php: <?php $file = "path/to/the/file.jpg"; header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Disposition: attachment; filename=".basename(str_replace(" ", "", $file))); header("Content-Description: File Transfer"); header('Accept-Ranges: bytes'); header('Content-Length: ' . filesize($file)); @readfile($file); ?> That should automatically open a 'save as' box in the client browser. You could also pass which file to download in $_GET variables, but be careful about security... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.