Why cant I download a file with ASP.NET [WriteFile]

  • Thread starter Thread starter rooster575
  • Start date Start date
R

rooster575

I am trying to allow a web user to download a file that exists in a
protected directory.

If I use the following sub, the contents of the file are written to the web
page, instead of prompting the user to save the file.

What am I doing wrong?

-R

Sub doDownload()
Response.ContentType = "application\msword"
Dim filename As String = New String("c:\\Temp\\myDownload\\hi.doc")
Dim downloadFile As System.IO.FileStream = New
System.IO.FileStream(filename, IO.FileMode.Open)
Response.Write(downloadFile.Length() & "#")
downloadFile.Close()
Response.WriteFile(filename)
Response.Flush()
Response.End()
End Sub
 
Nothing. It is the client that decides how to handle different content types. Usually though, if a client has MS Word installed it either opens up in the web page, or opens up in Word itself. You can set this in Windows Explorer, under Tools... Folder Options ->File Types tab; look for the .doc extension and select Advanced, the check boxes there are self explanatory.
If the user does not have MS Word installed, the file download dialog will come up. There is a header that can be sent to force this, but it escapes me at the moment!

hope this helps,
sivilian
 
Add a line of code like this:
Response.AddHeader("Content-Disposition","attachment;filename=hi.doc");

The "attachment" part asks the browser to open the file outside of the
browser.
 
Back
Top