Jump to content

start save as dialog from button


Jim from Oakland

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/71863-start-save-as-dialog-from-button/
Share on other sites

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

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.