Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / December 2003

Tip: Looking for answers? Try searching our database.

Using a Stream to Download Excel Files

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ali - 29 Dec 2003 20:55 GMT
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
Carl Prothman [MVP] - 29 Dec 2003 22:28 GMT
> I need a functionality where my clients download Excel files and
> after they do, I do some processing.  Downloading is easily achieved
[quoted text clipped - 4 lines]
>
> 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.

Signature

Thanks,
Carl Prothman
Microsoft ASP.NET MVP

Ali - 29 Dec 2003 23:01 GMT
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

> > I need a functionality where my clients download Excel files and
> > after they do, I do some processing.  Downloading is easily achieved
[quoted text clipped - 8 lines]
> You are missing the ContentType setting.
> Response.ContentType = "application/vnd.ms-excel"

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

> Also, please don't cross-post to so many newsgroups.
>
[quoted text clipped - 3 lines]
> Carl Prothman
> Microsoft ASP.NET MVP
Steve C. Orr [MVP, MCSD] - 29 Dec 2003 23:14 GMT
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/frlr
fsystemwebhttpresponseclasswritefiletopic.asp

http://www.aspnetpro.com/NewsletterArticle/2003/09/asp200309so_l/asp200309so_l.asp

Signature

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

> Thanks Carl and I am sorry for cross posting.  I just did not know any
> better.
[quoted text clipped - 4 lines]
>
> Ali

http://support.microsoft.com/default.aspx?scid=kb;en-us;318756&Product=aspne
> t
>
[quoted text clipped - 10 lines]
> > You are missing the ContentType setting.
> > Response.ContentType = "application/vnd.ms-excel"

http://support.microsoft.com/default.aspx?scid=kb;en-us;318756&Product=aspne
> t
> >
[quoted text clipped - 5 lines]
> > Carl Prothman
> > Microsoft ASP.NET MVP
Ali - 30 Dec 2003 00:33 GMT
Exactly what I needed Steve.  Thanks Dude.
Ali
> 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,
[quoted text clipped - 11 lines]
> >
> > Ali

http://support.microsoft.com/default.aspx?scid=kb;en-us;318756&Product=aspne
> > t
> >
[quoted text clipped - 10 lines]
> > > You are missing the ContentType setting.
> > > Response.ContentType = "application/vnd.ms-excel"

http://support.microsoft.com/default.aspx?scid=kb;en-us;318756&Product=aspne
> > t
> > >
[quoted text clipped - 5 lines]
> > > Carl Prothman
> > > Microsoft ASP.NET MVP

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.