Link to home
Start Free TrialLog in
Avatar of lplasc
lplasc

asked on

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()
Avatar of JuanCarniglia
JuanCarniglia
Flag of Argentina image

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.
Avatar of lplasc
lplasc

ASKER

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()
ASKER CERTIFIED SOLUTION
Avatar of JuanCarniglia
JuanCarniglia
Flag of Argentina image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lplasc

ASKER

Thank you for the answer.