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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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