473,323 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

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

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
Nov 18 '05 #1
2 1317
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
"rooster575" wrote:
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

Nov 18 '05 #2
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.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net

"rooster575" <ro********@hotmail.com> wrote in message
news:Ov*************@TK2MSFTNGP09.phx.gbl...
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

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Nimesh | last post by:
Hi All, I am new to ASP.NET. I need to download a file from server to the client machine. Any ideas on how to go about this ? Thanks In Advance.
2
by: Tom Youngquist | last post by:
I am trying to download a text file that my .NET page has just created based on entered parameters on the web page. Everything seems to work and the file is created. I am using the following code...
14
by: James Wong | last post by:
Hi! everybody, I'm facing a quite strange download problem. I use the following code to download an XML file to client side: With Response ' clear buffer Call .Clear() ' specify the...
11
by: John Smith Jr. | last post by:
Is there a way to have the server send a file to client (ie download) from a button click and a filename string?
4
by: MW de Jager | last post by:
Hi I want to make a download link on my ASP page, however if I use a Hyperlink, this just tries to open the file in the browser and does nothing if the file is of an unknown type. If I right...
2
by: Ken Varn | last post by:
I have an ASP.NET page that incorporates the following code on a button press. private void DownloadTag_Command(object sender, CommandEventArgs e) { FileStream fs; String Filename; Filename...
4
by: tjfdownsouth | last post by:
I have a site that allows a person to log in and get a list of files to download for them. If the file size is under 65MB then everything is fine, they click the download button and the save box...
3
by: tshad | last post by:
I have a function that downloads a file to the users computer and it works fine. The problem is that I then want the program to rename the file (file.move) to the same name plus todays date. ...
5
by: Khafancoder | last post by:
Hi guys, i am building a FileSharing website, i wanna allow users to be able using download managers such as DAP to download files from webserver but don't allow them to retrive files url......
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.