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?
 
JuanCarnigliaConnect With a Mentor Commented:
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
 
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
 
lplascAuthor Commented:
Thank you for the answer.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.