• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 361
  • Last Modified:

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()
0
lplasc
Asked:
lplasc
  • 2
  • 2
1 Solution
 
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
 
lplascAuthor Commented:
Thank you for the answer.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now