ASP.Net - Trouble Prompting Excel Download

I want to prompt the user to Save or Cancel a download of an excel file using the following asp.net code.  The file opens up as a spreadsheet instead of prompting the user to Save which I would like.  Any suggestions would be welcome.

            Dim fs As FileStream
            Dim strcontenttype As String
            Dim strpath = "\\server1\Folder1\"
            Dim strfilename As String = "file1.xls"
            Response.Clear()
            fs = File.Open(strpath & strfilename, FileMode.Open)
            Dim bytbytes(fs.Length) As Byte
            fs.Read(bytbytes, 0, fs.Length)
            fs.Close()
            Response.AddHeader("content-dispostion", "attachment; filename=" & strfilename)
            Response.ContentType = "application/vnd.ms-excel"
            Response.Charset = "UTF-8"
            Response.ContentType = ContentType
            Response.BinaryWrite(bytbytes)
            Response.End()
lplascAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JuanCarnigliaCommented:
This line:

Response.ContentType = "application/vnd.ms-excel"

Is what tells the browser to treat this file as Excel, and attempt to open it.

Maybe replace this for :

Response.ContentType = "application/octet-stream"

Just a thought, hope it helps.
0
lplascAuthor Commented:
I tried using "Response.ContentType = "application/octet-stream" and I got a "An invalid character was found in text content. Error processing resource" error.

By the way, there is an extra line in my code--"Response.ContentType = ContentType" that I am removing.

Here is the listing again:

            Dim fs As FileStream
            Dim strcontenttype As String
            Dim strpath = "\\server1\Folder1\"
            Dim strfilename As String = "file1.xls"

            Response.Clear()

            fs = File.Open(strpath & strfilename, FileMode.Open)
            Dim bytbytes(fs.Length) As Byte
            fs.Read(bytbytes, 0, fs.Length)
            fs.Close()

            Response.AddHeader("content-dispostion", "attachment; filename=" & strfilename)
            Response.ContentType = "application/vnd.ms-excel"
            Response.Charset = "UTF-8"
            Response.BinaryWrite(bytbytes)

            Response.End()
0
JuanCarnigliaCommented:
I copied the code on my machine and tested it with Firefox and IE7. It worked, in both cases it prompted me for an action !!..

This is the code:
<%
            Dim strcontenttype
 
            Response.Clear()
 
Dim s2 As New System.IO.FileStream("C:\test1.xls", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)
 
            
            Dim bytbytes(s2.Length) as Byte
            s2.Read(bytbytes, 0, s2.Length)
            s2.Close()
 
            Response.AddHeader("content-dispostion", "attachment; filename=test1.xls")
            Response.ContentType = "application/vnd.ms-excel"
            Response.Charset = "UTF-8"
            Response.BinaryWrite(bytbytes)
 
            Response.End()
%>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lplascAuthor Commented:
Thank you for the answer.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.