Reputation: 1
Using VB.NET, we write a .xlsx (Excel Workbook) file to disk and then read into a byte array. Opening the Excel Workbook using File Explorer works correctly, but writing the byte array to the Response output stream and opening the downloaded file in any browser gives an error message (see below).
This error does not occur on regular Excel (.xls) files.
Microsoft Excel: We found a problem with some content in 'xxx.xlsx'. Do you want us to recover as much as we can?
Dim tB As Byte() = File.ReadAllBytes(diskOpts.DiskFileName)
Response.Clear()
Response.Buffer = True
Response.ContentType = sUtilities.Instance.GetMimeType(tExtens)
Response.AddHeader("Content-Disposition", "filename=" & fRep.Name & "." & tExtens)
Response.OutputStream.Write(tB, 0, tB.Length) `
Upvotes: 0
Views: 222
Reputation: 11773
Have you tried it like this?
Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", fileName))
Response.ContentType = "application/vnd.ms-excel"
Response.WriteFile(fileName)
EDIT
I looked at our website that allows users to download a variety of files. This code works for .xslx files.
Dim fi As New IO.FileInfo(docPath)
Response.Clear()
Response.ClearHeaders()
Response.ClearContent()
Dim ext As String = IO.Path.GetExtension(docPath).ToLower
If ext = ".pdf" Then
Response.ContentType = "application/pdf"
Else
Response.ContentType = "application/octet-stream"
End If
Response.AppendHeader("Content-Disposition", "attachment; filename=" & IO.Path.GetFileName(docPath))
Response.AppendHeader("Content-Length", fi.Length.ToString())
Response.BufferOutput = True
Response.TransmitFile(docPath)
Response.End()
Upvotes: 1