Click to See Complete Forum and Search --> : Help!! Download file dialog


fongjeffrey
May 26th, 2005, 04:30 AM
Hi there,
I am having problem to show the File Dialog for downloading a file from the server. I need to display a file dialog, so that the user can select where he/she wants to save the file. The followings are the code that on my download.aspx file. "path" is the complete path of the file being downloaded.


string path = (string)Request.QueryString["File"];
System.IO.FileInfo file = new System.IO.FileInfo(path);

Response.Clear();

Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

Response.AddHeader("Content-Length", file.Length.ToString());

Response.ContentType = "application/octet-stream";

Response.Flush();
Response.Write(file.FullName);
Response.End();

I don't know what's wrong with my code. Could anyone tell me how to chnage my code and the file dialog can be displayed for selecting the store location?

Any help will be appreicated! Thank you very much

Jeffrey

mickey229
May 30th, 2005, 07:21 AM
'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>