That is because you're setting
Response.ContentType = "application/pdf";
That tell's Chrome the file is a PDF, and then it opens it with PDFjs, and when you click on save, it wants to save the PDFjs-Viewer web-Page instead of the PDF.
If you click on the download-icon, then you can download the PDF.
You need to set content-disposition "attachment", and you need to set the mime-type to application/octet-stream. Then it works in Chrome (and IE and Firefox).
If you want to display the PDF in a viewer in IE just like in Chrome, then you need to set content-disposition inline (IE ignores the mime-type and goes for the file extension instead).
Dim baPDF As Byte() = GetPdfFromImage(Me.Data)
context.Response.Clear()
'context.Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName)
context.Response.AddHeader("Content-Disposition", Portal.ASP.NET.GetContentDisposition("Drucken.pdf", "inline"))
context.Response.AddHeader("Content-Length", baPDF.Length.ToString())
' context.Response.ContentType = "application/msword"
' context.Response.ContentType = "application/octet-stream"
' http://superuser.com/questions/219870/how-to-open-pdf-in-chromes-integrated-viewer-without-downloading-it#
' context.Response.ContentType = "text/html"
context.Response.ContentType = "application/pdf"
Also, be aware if you have UTF-8 characters in the filename, you need to properly set the filename header (different values for different browsers in different versions [IE]).
Public Shared Function GetContentDisposition(ByVal strFileName As String) As String
Return GetContentDisposition(strFileName, "attachment")
End Function ' GetContentDisposition '
' http://www.iana.org/assignments/cont-disp/cont-disp.xhtml '
Public Shared Function GetContentDisposition(ByVal strFileName As String, ByVal strDisposition As String) As String
' http://stackoverflow.com/questions/93551/how-to-encode-the-filename-parameter-of-content-disposition-header-in-http '
Dim contentDisposition As String
strFileName = StripInvalidPathChars(strFileName)
If String.IsNullOrEmpty(strDisposition) Then
strDisposition = "inline"
End If
If System.Web.HttpContext.Current IsNot Nothing AndAlso System.Web.HttpContext.Current.Request.Browser IsNot Nothing Then
If (System.Web.HttpContext.Current.Request.Browser.Browser = "IE" And (System.Web.HttpContext.Current.Request.Browser.Version = "7.0" Or System.Web.HttpContext.Current.Request.Browser.Version = "8.0")) Then
contentDisposition = strDisposition + "; filename=" + Uri.EscapeDataString(strFileName).Replace("'", Uri.HexEscape("'"c))
ElseIf (System.Web.HttpContext.Current.Request.Browser.Browser = "Safari") Then
contentDisposition = strDisposition + "; filename=" + strFileName
Else
contentDisposition = strDisposition + "; filename*=UTF-8''" + Uri.EscapeDataString(strFileName)
End If
Else
contentDisposition = strDisposition + "; filename*=UTF-8''" + Uri.EscapeDataString(strFileName)
End If
Return contentDisposition
End Function ' GetContentDisposition '
Also, if you're open to good advice, get rid of
Session["empcd"]
You're setting yourselfs up for NullReferenceExceptions, and it's entirely unnecessary...
Try not to use sessions, if you can avoid it.
Oh, and by the way:
You should add a using clause to your WebClient; otherwise you're not properly disposing of resources.
using(WebClient client = new WebClient())
{
// Your code goes here
} // End Using
That will have the added benefit over client.Dispose() that you don't need to put it into a try-cach if-not-null-dispose to assure resources are freed afterwards.