Save As Dialog Box

Discussion in 'ASP General' started by rn5a@rediffmail.com, Apr 4, 2007.

  1. Guest

    When users submit a Form with a SQL query, the resultset is retrieved
    from a MS-Access database table which is then exported to an MS-Excel
    worksheet. After the records get exported to MS-Excel, I want to give
    users the option to download the Excel file to their local machine. In
    other words, when users click a download link, I want the Save As..
    dialog box to pop-up so that users can save the Excel file to their
    local machine (note that I don't want users to view the Excel
    worksheet in their browser). This is how I tried it:

    <%
    Dim strFile
    'here strFile is just the file name & does not include it's
    physical path
    strFile=Request("file")

    Response.ContentType="application/octetstream"
    Response.AddHeader "Content-Disposition","filename=" & strFile

    Dim objStream
    strFile=Server.MapPath(strFile)

    Set objStream=Server.CreateObject("ADODB.STREAM")
    objStream.Open
    objStream.Type=1
    objStream.LoadFromFile(strFile)

    Response.BinaryWrite(objStream.Read)

    objStream.Close
    Set objStream=Nothing
    %>

    The above code doesn't give users the option to save the Excel file in
    their local machines. Rather the Excel worksheet opens in the browser.

    What am I doing wrong here?

    What I find is if I pass the PHYSICAL PATH of the Excel file to the
    LoadFromFile method, say, something like C:\Inetpub\wwwroot\XL
    \XLFile.xls & change the above code slightly to look like this:

    <%
    Dim strFile,strFileName
    strFile=Request("file")
    strFile="C:\Inetpub\wwwroot\XL\" & strFile

    'just get the file name; exclude it's path
    strFileName=Right(strFile,Len(strFile)-InStrRev(strFile,"\"))

    Response.ContentType="application/octetstream"
    Response.AddHeader "Content-Disposition","filename=" & strFileName

    Dim objStream
    Set objStream=Server.CreateObject("ADODB.STREAM")
    objStream.Open
    objStream.Type=1
    objStream.LoadFromFile(strFile)

    Response.BinaryWrite(objStream.Read)

    objStream.Close
    Set objStream=Nothing
    %>

    In the 2nd code, I am passing the file name as well as it's entire
    PHYSICAL PATH to the LoadFromFile method & this does pop-up the Save
    As... dialog box so that users can download the Excel file to their
    local machines.

    But in the 1st code, I am passing the file name along with it's
    VIRTUAL PATH to the LoadFromFile method. Hence to get the PHYSICAL
    PATH, I am using Server.MapPath but the Excel file opens up in the
    browser & doesn't pop-up the Save As.. dialog box.
     
    , Apr 4, 2007
    #1
    1. Advertising

  2. Guest

    On Apr 4, 4:55 am, wrote:
    > When users submit a Form with a SQL query, the resultset is retrieved
    > from a MS-Access database table which is then exported to an MS-Excel
    > worksheet. After the records get exported to MS-Excel, I want to give
    > users the option to download the Excel file to their local machine. In
    > other words, when users click a download link, I want the Save As..
    > dialog box to pop-up so that users can save the Excel file to their
    > local machine (note that I don't want users to view the Excel
    > worksheet in their browser). This is how I tried it:
    >
    > <%
    > Dim strFile
    > 'here strFile is just the file name & does not include it's
    > physical path
    > strFile=Request("file")
    >
    > Response.ContentType="application/octetstream"
    > Response.AddHeader "Content-Disposition","filename=" & strFile
    >
    > Dim objStream
    > strFile=Server.MapPath(strFile)
    >
    > Set objStream=Server.CreateObject("ADODB.STREAM")
    > objStream.Open
    > objStream.Type=1
    > objStream.LoadFromFile(strFile)
    >
    > Response.BinaryWrite(objStream.Read)
    >
    > objStream.Close
    > Set objStream=Nothing
    > %>
    >
    > The above code doesn't give users the option to save the Excel file in
    > their local machines. Rather the Excel worksheet opens in the browser.
    >
    > What am I doing wrong here?
    >
    > What I find is if I pass the PHYSICAL PATH of the Excel file to the
    > LoadFromFile method, say, something like C:\Inetpub\wwwroot\XL
    > \XLFile.xls & change the above code slightly to look like this:
    >
    > <%
    > Dim strFile,strFileName
    > strFile=Request("file")
    > strFile="C:\Inetpub\wwwroot\XL\" & strFile
    >
    > 'just get the file name; exclude it's path
    > strFileName=Right(strFile,Len(strFile)-InStrRev(strFile,"\"))
    >
    > Response.ContentType="application/octetstream"
    > Response.AddHeader "Content-Disposition","filename=" & strFileName
    >
    > Dim objStream
    > Set objStream=Server.CreateObject("ADODB.STREAM")
    > objStream.Open
    > objStream.Type=1
    > objStream.LoadFromFile(strFile)
    >
    > Response.BinaryWrite(objStream.Read)
    >
    > objStream.Close
    > Set objStream=Nothing
    > %>
    >
    > In the 2nd code, I am passing the file name as well as it's entire
    > PHYSICAL PATH to the LoadFromFile method & this does pop-up the Save
    > As... dialog box so that users can download the Excel file to their
    > local machines.
    >
    > But in the 1st code, I am passing the file name along with it's
    > VIRTUAL PATH to the LoadFromFile method. Hence to get the PHYSICAL
    > PATH, I am using Server.MapPath but the Excel file opens up in the
    > browser & doesn't pop-up the Save As.. dialog box.


    OK....I got it. The line

    Response.AddHeader "Content-Disposition","filename=" & strFile

    should read as

    Response.AddHeader "Content-Disposition","attachment;filename=" &
    strFile
     
    , Apr 4, 2007
    #2
    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. =?Utf-8?B?Q2hhbg==?=

    Display save dialog box in ASP.net page

    =?Utf-8?B?Q2hhbg==?=, Apr 28, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    854
    Natty Gur
    Apr 29, 2004
  2. Natty Gur
    Replies:
    0
    Views:
    3,642
    Natty Gur
    Apr 29, 2004
  3. Eilsa

    Save Dialog Box character set

    Eilsa, Aug 5, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    368
    Eilsa
    Aug 5, 2004
  4. BusyBoy
    Replies:
    2
    Views:
    609
    =?Utf-8?B?TGVvbiBNYXluZQ==?=
    Oct 31, 2006
  5. hell2heaven
    Replies:
    0
    Views:
    421
    hell2heaven
    Oct 26, 2008
Loading...

Share This Page