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("conten t-disposti on", "attachment; filename=" & strfilename)
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = "UTF-8"
Response.ContentType = ContentType
Response.BinaryWrite(bytby tes)
Response.End()
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("conten
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = "UTF-8"
Response.ContentType = ContentType
Response.BinaryWrite(bytby
Response.End()
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.ContentTyp e = 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("conten t-disposti on", "attachment; filename=" & strfilename)
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = "UTF-8"
Response.BinaryWrite(bytby tes)
Response.End()
By the way, there is an extra line in my code--"Response.ContentTyp
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("conten
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = "UTF-8"
Response.BinaryWrite(bytby
Response.End()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for the answer.
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.