how to write html tag to download a file intead of show content of file in browser

Discussion in 'HTML' started by yihucd@gmail.com, Feb 8, 2006.

  1. Guest

    I am write a html page that has one link on it. The purpose of the link
    is to popup a window to let the user download/save the Excel file to
    his/her local computer. But by using my code, when I click the link
    "file" in the page, the content of the excel file showed in the browser
    (I have firefox as my defult browser).

    What I want is that : no matter what the user's default browser is,
    when user click the link in the page, the browser should always pop up
    a window to let the user save the file.
    (because it is annoying to show some garbage in the browser window)

    Thanks,

    David



    ------------------------------------------my code --------
    <html>

    <head>
    <meta http-equiv="Content-Language" content="en-us">
    <meta http-equiv="Content-Type" content="text/html;
    charset=windows-1252">
    <title>click file</title>
    </head>

    <body>

    <p>click <a href="xls_to_del.xls">file</a></p>

    </body>

    </html>
     
    , Feb 8, 2006
    #1
    1. Advertising

  2. wrote:

    > I am write a html page that has one link on it. The purpose of the link
    > is to popup a window to let the user download/save the Excel file to
    > his/her local computer.


    <a href="foo.xls">Link Text</a>

    > But by using my code, when I click the link "file" in the page, the
    > content of the excel file showed in the browser


    Then either your server is misconfigured and is claiming that your Excel
    file is plain text (or HTML or something else that isn't Excel), or your
    browser has the Excel plugin and is configured to use it.

    > What I want is that : no matter what the user's default browser is,
    > when user click the link in the page, the browser should always pop up
    > a window to let the user save the file.


    You /can/ use the Content-Disposition header to ask the browser to treat it
    as an attachment rather than something to be displayed ... but I don't know
    of any browser which doesn't have an option for the user to save the target
    of a link instead of opening it with the default handler. If the user has
    their browser set up to open Excel files in a plugin, then its likely to be
    worth respecting that.

    http://www.faqs.org/rfcs/rfc2183.html

    > (because it is annoying to show some garbage in the browser window)


    Sounds like its a case of the server claiming the Excel file is plain text.

    --
    David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
    Home is where the ~/.bashrc is
     
    David Dorward, Feb 8, 2006
    #2
    1. Advertising

  3. Neredbojias Guest

    With neither quill nor qualm, quothed:

    > I am write a html page that has one link on it. The purpose of the link
    > is to popup a window to let the user download/save the Excel file to
    > his/her local computer. But by using my code, when I click the link
    > "file" in the page, the content of the excel file showed in the browser
    > (I have firefox as my defult browser).
    >
    > What I want is that : no matter what the user's default browser is,
    > when user click the link in the page, the browser should always pop up
    > a window to let the user save the file.
    > (because it is annoying to show some garbage in the browser window)


    Zip it.

    --
    Neredbojias
    Contrary to popular belief, it is believable.
     
    Neredbojias, Feb 9, 2006
    #3
  4. Toby Inkster Guest

    yihucd wrote:

    > (because it is annoying to show some garbage in the browser window)


    Configure your server to send it with the correct MIME type, which is:

    application/vnd.ms-excel

    If you don't know how to do this, ask your host.

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me ~ http://tobyinkster.co.uk/contact
     
    Toby Inkster, Feb 9, 2006
    #4
  5. Guest

    Thank you all so much. I put following code in my JSP code.
    But the thing is that, when I load this page, I only see the pop up
    window without first seeing the acutal page.

    What I want is a page showing the following sentence

    " Click file"

    When the user click on the word file, then a window pop up. How to do
    this?

    <%
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-Disposition","attachment; filename=" +
    "xls_to_del.xls" );
    %>


    <html>

    <head>
    <title>click file</title>
    </head>

    <body>

    <p>click <a href="xls_to_del.xls">file</a></p>

    </body>

    </html>
     
    , Feb 9, 2006
    #5
  6. wrote:

    > <%
    > response.setContentType("application/vnd.ms-excel");
    > response.setHeader("Content-Disposition","attachment; filename=" +
    > "xls_to_del.xls" );
    > %>
    > <p>click <a href="xls_to_del.xls">file</a></p>


    You need to send the Content-type that says "This is an Excel file!" when
    you send the Excel file, not when you send an HTML document with a link to
    the Excel file in it.

    --
    David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
    Home is where the ~/.bashrc is
     
    David Dorward, Feb 9, 2006
    #6
  7. Guest

    Hi David Dorward,

    Can you detail how to

    "send the Content-type that says "This is an Excel file!" when
    you send the Excel file"

    ?
     
    , Feb 9, 2006
    #7
  8. wrote:

    > "send the Content-type that says "This is an Excel file!" when
    > you send the Excel file"


    Assuming you are using JSP, do exactly what you were doing, but replace the
    HTML with the Excel data.

    --
    David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
    Home is where the ~/.bashrc is
     
    David Dorward, Feb 9, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shruds
    Replies:
    1
    Views:
    961
    John C. Bollinger
    Jan 27, 2006
  2. Brett  Kelly
    Replies:
    1
    Views:
    728
    Steve C. Orr [MVP, MCSD]
    Jun 16, 2006
  3. Replies:
    1
    Views:
    424
    Andrew Thompson
    Nov 29, 2006
  4. bhabs
    Replies:
    2
    Views:
    453
    Tad J McClellan
    Feb 13, 2008
  5. Replies:
    11
    Views:
    771
Loading...

Share This Page