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

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
 
G

Guest

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
 
S

Steve C. Orr [MVP, MCSD]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top