Open  a Save/Download dialog using javascript

Posted on 2006-06-07
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
    LVL 40

    Expert Comment

    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

    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

    You say "inbox". Can you explain what you mean by this?


    Author Comment

    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

    Right. So you have a URL for the attachment (say - 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

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    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.

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now