Open  a Save/Download dialog using javascript

Posted on 2006-06-07
Medium Priority
Last Modified: 2013-12-13
hi all,
iam doing a mail interface using PHP .
i just want to popup the save/download dialog box on clicking the attachments in the inbox and save it to the local disk.

Please help me out.

Thanks in advance

Question by:lword
  • 3
  • 2
LVL 40

Expert Comment

by:Richard Quadling
ID: 16850772
Hi lword,

You can't directly. You have to provide a download. When you click the link, you need to use PHP's header() function to send at least 2 headers.

1 - header('Content-type: xxx');
2 - header('Content-length: yyy');

xxx is the type of file being downloaded. If you don't want the browser to process it (either itself or by a plugin) you need to provide a type which is raw data. Maybe application/octet-stream.

yyy is the length of the data being sent. This is useful to show the user how long they have left to download.

Then you would use ...


where zzz is the name of the file.


Richard Quadling.

Author Comment

ID: 16850833
Thanks Richard Quadling.

is it possible to do it in client-side i.e using Javascript / vbscript.?

i want the user to download the attachment
which is stored in the web server ,(and displayed in the inbox as attachments) to their
local hard disk by invoking the  open/save download dialog box.

LVL 40

Expert Comment

by:Richard Quadling
ID: 16850865
You say "inbox". Can you explain what you mean by this?


Author Comment

ID: 16859665
hi  RQuadling

i'm developing a mail interface like gmail,yahoo etc.
Inbox is the place in which all the incoming mails , from the mail server , are displayed.
the incoming  mails from the mailserver is downloaded to the database and shown as inbox in the browser.
If the incoming mails received contains attachments, Iam storing it in the web server and displaying the file name
as attachment in the inbox.I want the user to download the attachments to the client (local hard disk by prompting the open/save disalog box ), by clicking on the attachment link in the inbox.
LVL 40

Accepted Solution

Richard Quadling earned 2000 total points
ID: 16859704
Right. So you have a URL for the attachment (say http://www.yoursite.com/attachment.php?id=xxxxxxx - where xxxxx is the id assigned to that attachment).

To get the user to download the file, they click it. This runs attachment.php and that looks like ...
if (isset($_GET['id']) && somevalidation($_GET['id']))
 $s_filename = someconversion($_GET['id']);
 if (file_exists($s_filename))
  header('Content-disposition: attachment; filename=' . basename($s_filename));
  header('Content-lengh: ' . filesize($s_filename);
 echo <<< END_HTML
<title>Missing attachment</title>
<h1>Missing attachment</h1>

You will need to write code to validate that the ID supplied is appropriate to the user and code to convert the ID to a real file. The filename used to hold the file on the server MAY not be the filename for the file for the user. In which case that will need to be dealt with too.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month16 days, 17 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question