• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 943
  • Last Modified:

Open a Save/Download dialog using javascript

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

0
lword
Asked:
lword
  • 3
  • 2
1 Solution
 
Richard QuadlingSenior Software DeveloperCommented:
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 ...

readfile(zzz);

where zzz is the name of the file.




Regards,

Richard Quadling.
0
 
lwordAuthor Commented:
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.

Thanks
lword
0
 
Richard QuadlingSenior Software DeveloperCommented:
You say "inbox". Can you explain what you mean by this?

0
 
lwordAuthor Commented:
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.
 
thanks
lword
0
 
Richard QuadlingSenior Software DeveloperCommented:
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 ...
<?php
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);
  readfile($s_filename);
  exit;
  }
 }
else
 {
 echo <<< END_HTML
<html>
<head>
<title>Missing attachment</title>
</head>
<body>
<h1>Missing attachment</h1>
Sorry.
</body>
</html>
END_HTML;
?>


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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now