Sat. Jul 13th, 2024

有使用ReportViewer匯出PDF報表的注意了

Chrome更新53.0.2785.116版本之後
利用ReportViewer把匯出PDF報表功能寫在Button內
會發生錯誤

Dim warnings As Warning()
Dim streamids As String()
Dim mimeType As String
Dim encoding As String
Dim extension As String

Dim bytes As Byte() = ReportViewer1.LocalReport.Render("Pdf", Nothing, mimeType, encoding, extension, streamids, warnings)

Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=BTrip-" & GridView1.SelectedValue & ".pdf")
Response.AddHeader("Accept-Header", reportBytes.Length.ToString())
Response.ContentType = "application/octet-stream"
Response.OutputStream.Write(bytes, 0, bytes.Length)

把匯出程式寫在Button,利用ReportViewer匯出PDF網路上很多人寫過,如果你COPY到的程式是下面這行,USER剛好又使用Chrome 53.0.2785.116下載時就會發生錯誤。

Response.AddHeader("Accept-Header", reportBytes.Length.ToString())

實測發現把上面程式碼改成下面這行就可以正常下載了。

Response.AddHeader("Content-Length", reportBytes.Length.ToString())