I’m pulling PDF files from a SQL table, and then wanting to show the PDF in a new window rather than download to the user’s computer. I get the data successfully, but when I try to view the PDF I get blank pages. The right number of pages shows but content is blank. Can’t understand what’s happening. Here is my code:
var ms = new Date();
var params = "action=DownloadFile2&ms=" + ms.getTime() + "&file_id=" + file_id;
var url = urlPath + params;
$.ajax({
type: "POST",
method: "POST",
responseType: "blob",
url: url,
success: function (data) {
var file = new Blob([data], { type: 'application/pdf' });
var fileURL = URL.createObjectURL(file);
window.open(fileURL, '', "height=1000, width=2000");
}
Code behind: If IsDBNull(dt) = True Then Exit Sub
Dim filename As String
filename = Trim(dt.Rows(0)("Name").ToString())
Dim filetype As String
filetype = Trim(dt.Rows(0)("File_Type").ToString())
Dim encodedBytes As Byte() = Convert.FromBase64String(Convert.ToBase64String(CType(dt.Rows(0)("Data"), Byte())))
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.AddHeader("Pragma", "public")
HttpContext.Current.Response.AddHeader("Cache-Control", "max-age=0")
HttpContext.Current.Response.AddHeader("Content-disposition", "attachment;filename=" & filename)
HttpContext.Current.Response.ContentType = "application/pdf"
HttpContext.Current.Response.SuppressContent = False
Response.Headers.Add("Content-Length", encodedBytes.Length.ToString())
Dim outputStream As Stream = HttpContext.Current.Response.OutputStream
outputStream.Write(encodedBytes, 0, encodedBytes.Length)
outputStream.Flush()
outputStream.Close()
I tried to open the PDF in a new window, I just get blank pages. One document showed page 1 and rest blank.