Results 1 to 6 of 6

Thread: Help Downloading From Web Page (RESOLVED)

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Location
    Covina, Ca
    Posts
    5

    Resolved Help Downloading From Web Page (RESOLVED)

    Hi. I'm a nube to asp.net. I'm trying to get a file to download
    from my web page using Response.AddHeader. The Download
    Dialog attempts to download the Web Page 'Download.aspx'
    instead of the File I am selecting.

    Code:

    'Put this is a code behind module or asp.net page
    Sub DisplayDownloadDialog(ByVal PathVirtual As String)

    Dim strPhysicalPath As String
    Dim objFileInfo As System.IO.FileInfo
    Try
    strPhysicalPath = Server.MapPath(PathVirtual)
    'exit if file does not exist
    If Not System.IO.File.Exists(strPhysicalPath) _
    Then Exit Sub
    objFileInfo = New System.IO.FileInfo(strPhysicalPath)
    Response.Clear()
    'Add Headers to enable dialog display
    Response.AddHeader("Content-Disposition", "attachment; filename=" & _
    objFileInfo.Name)
    Response.AddHeader("Content-Length", objFileInfo.Length.ToString())

    Response.ContentType = "application/octet-stream"
    Response.WriteFile(objFileInfo.FullName)


    Catch
    'on exception take no action
    'you can implement differently
    Finally

    Response.End()

    End Try
    End Sub
    'DEMO
    'IN YOUR CODE BEHIND PAGE:
    'DECLARATION
    Protected WithEvents btnDownload As _
    System.Web.UI.WebControls.Button

    'IN CODE
    'ASSUMES MYWORDFILE.DOC EXISTS IN SAME FOLDER
    'AS THE ASPX FILE
    Private Sub btnDownload_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles btnDownload.Click
    DisplayDownloadDialog("MyWordFile.doc")
    End Sub
    'ON APSX PAGE
    <asp:Button id="btnDownload" runat="server" Text="Download a Word File"></asp:Button>

    Any help would be appreciated!

    Thanks

    jayare
    Last edited by jayare1948; Sep 23rd, 2004 at 01:50 PM.

  2. #2
    Member
    Join Date
    Sep 2004
    Location
    Oklahoma City, OK
    Posts
    36
    Seems like you're taking a very complicated approach to a simple matter. Unless you're dynamically writing data from a data source, such as a server-side implementation of a Word object, the method you're using is overly complex.

    Instead, try implementing something similar to this:
    VB Code:
    1. Private Sub DisplayDonwloadDialog(ByVal PathVirtual As String)
    2.     Dim strPhysicalPath As String = Server.MapPath(PathVirtual)
    3.     If (System.IO.File.Exists(strPhysicalPath)) Then
    4.         Response.Redirect(PathVirtual)
    5.     Else
    6.         Response.Redirect("DownloadErrorPage.aspx")
    7.     End If
    8. End Sub

    The remainder of your code would remain the same, but your client would be able to download the object with IIS handling all of the necessary headers and content.

    T

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Location
    Covina, Ca
    Posts
    5

    Question

    Tcorey08:

    Thanks for the response.
    I tried your code and as I expcted

    Response.Redirect(PathVirtual)

    simply displays the content of the document I am trying to download.

    I need an instance of the download dialog that selects
    the right file for download to the client side.


    jayare1948

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Location
    Covina, Ca
    Posts
    5

    Question

    Tcorey08:

    Thanks for the response.
    I tried your code and as I expcted

    Response.Redirect(PathVirtual)

    simply displays the content of the document I am trying to download.

    I need an instance of the download dialog that selects
    the right file for download to the client side.


    jayare1948

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Location
    Covina, Ca
    Posts
    5
    TCorrey08:

    Further research turned up a CRITICAL but missing piece
    of my code sample.

    Prior to the Response.AddHeaders there needs to be
    a Response.ClearHeaders().

    Here's my final code that works:

    Sub DisplayDownloadDialog(ByVal PathVirtual As String)
    Dim strPhysicalPath As String
    Dim objFileInfo As System.IO.FileInfo
    Try
    strPhysicalPath = Server.MapPath(PathVirtual)
    'Exit if file does not exist
    If Not System.IO.File.Exists(strPhysicalPath) _
    Then Exit Sub
    objFileInfo = New System.IO.FileInfo(strPhysicalPath)
    Response.ClearHeaders()
    Response.Clear()
    ' Add Headers to enable dialog display
    Response.AddHeader("Content-Disposition", "attachment; filename=" & _
    objFileInfo.Name)
    Response.AddHeader("Content-Length", objFileInfo.Length.ToString())

    Response.ContentType = "application/octet-stream"
    Response.WriteFile(objFileInfo.FullName)
    Catch
    'on exception take no action
    'you can implement differently
    Finally
    Response.End()
    End Try
    End Sub

  6. #6
    Member
    Join Date
    Sep 2004
    Location
    Oklahoma City, OK
    Posts
    36
    Good deal! Best of luck on future endeavors!
    Thomas Corey
    IT Engineering Consultant
    TALSoft Enterprises Inc.
    Oklahoma City, OK

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width