Results 1 to 6 of 6

Thread: Help Downloading From Web Page (RESOLVED)

Threaded View

  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.

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