Using a Stream to Download Excel Files

A

Ali

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
 
C

Carl Prothman [MVP]

Ali said:
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.

Response.AddHeader("Content-Disposition", "attachment; filename=" &

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.
 
S

Steve C. Orr [MVP, MCSD]

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/d...fsystemwebhttpresponseclasswritefiletopic.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
 
A

Ali

Exactly what I needed Steve. Thanks Dude.
Ali
Steve C. Orr said:
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.asphttp://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




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

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,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top