Groups | Blog | Home
all groups > dotnet setup > december 2003 >

dotnet setup : Using a Stream to Download Excel Files



Carl Prothman [MVP]
12/29/2003 2:28:25 PM
[quoted text, click to view]

Ali,
You are missing the ContentType setting.
Response.ContentType = "application/vnd.ms-excel"
http://support.microsoft.com/default.aspx?scid=kb;en-us;318756&Product=aspnet

Also, please don't cross-post to so many newsgroups.

--

Thanks,
Carl Prothman
Microsoft ASP.NET MVP

Ali
12/29/2003 2:55:59 PM
I need a functionality where my clients download Excel files and after they
do, I do some processing. Downloading is easily achieved using a anchor or
hyperlink tag, but that does not give me the full functionality I am after.
So, I came up with a way to download the files using a stream but after I
download the file and try to open it in Excel I get garbage.

This is the code I use for the download function. Please help if you have
any ideas or suggestions. Thanks in advance.

Ali
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles MyBase.Load

Dim path As String
Dim fs As New FileStream(path, FileMode.Open)
Dim byteRead As Integer

path = Server.MapPath(Request("fileToDownload"))

Response.ClearHeaders()
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=" &
Request("fileToDownload").ToString
Response.ContentType = "application/XYZ"

byteRead = fs.ReadByte()

While byteRead <> -1
Response.Write(byteRead)
byteRead = fs.ReadByte()
End While

fs.Close()

End Sub

Steve C. Orr [MVP, MCSD]
12/29/2003 3:14:06 PM
To go along with Carl's answer, you can use the Response.Writefile method.
Something like this should work:

Response.Clear();
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition","attachment;filename=myfile.xls");
Response.WriteFile("myfile.xls");

Here's more info:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebhttpresponseclasswritefiletopic.asp
http://www.aspnetpro.com/NewsletterArticle/2003/09/asp200309so_l/asp200309so_l.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able-consulting.com



[quoted text, click to view]

Ali
12/29/2003 5:01:59 PM
Thanks Carl and I am sorry for cross posting. I just did not know any
better.

Your response assumes that I need to create an excel file using ASP.NET.
The problem is that I already have the Excel file on the server. All I want
to do is download it. But not using the anchor nor the hyperlink tags.

Ali
http://support.microsoft.com/default.aspx?scid=kb;en-us;318756&Product=aspne
t


[quoted text, click to view]
http://support.microsoft.com/default.aspx?scid=kb;en-us;318756&Product=aspne
t
[quoted text, click to view]

Ali
12/29/2003 6:33:06 PM
Exactly what I needed Steve. Thanks Dude.
Ali
[quoted text, click to view]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemwebhttpresponseclasswritefiletopic.asp
[quoted text, click to view]
http://www.aspnetpro.com/NewsletterArticle/2003/09/asp200309so_l/asp200309so
_l.asp
[quoted text, click to view]

AddThis Social Bookmark Button