|
-
Sep 22nd, 2004, 04:28 PM
#1
Thread Starter
New Member
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.
-
Sep 22nd, 2004, 10:26 PM
#2
Member
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:
Private Sub DisplayDonwloadDialog(ByVal PathVirtual As String)
Dim strPhysicalPath As String = Server.MapPath(PathVirtual)
If (System.IO.File.Exists(strPhysicalPath)) Then
Response.Redirect(PathVirtual)
Else
Response.Redirect("DownloadErrorPage.aspx")
End If
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
-
Sep 23rd, 2004, 12:31 PM
#3
Thread Starter
New Member
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
-
Sep 23rd, 2004, 01:37 PM
#4
Thread Starter
New Member
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
-
Sep 23rd, 2004, 01:48 PM
#5
Thread Starter
New Member
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
-
Sep 23rd, 2004, 06:51 PM
#6
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|