locked
PDF Link in Chrome and Edge Not opening RRS feed

  • Question

  • User270757467 posted

    Hello,

    I have a web page that supplies a link to a PDF file we have on our web server. I pass the ID to the web page and it opens the PDF. This works fine on IE but in Chrome and Edge, it opens up the actual code file in Visual studio. How can I correct this? I have tried tweaking the PDF setting in Chrome without any luck.

    This is the PDF link that I call.... Clicking on this link for me will open VS. Doing the same in IE opens up the PDF.http://devlaspbsnet/OpenDoc.aspx?DocName=PSCPPSCPUSS07094414.pdf

    Thanks for any help.

    Tuesday, October 27, 2020 5:39 PM

All replies

  • User475983607 posted

    I have a web page that supplies a link to a PDF file we have on our web server. I pass the ID to the web page and it opens the PDF. This works fine on IE but in Chrome and Edge, it opens up the actual code file in Visual studio. How can I correct this?

    The symptom indicates a missing or incorrect content-type. 

    application/pdf

    https://stackoverflow.com/questions/41386977/return-file-equivalent-in-web-forms

    https://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-asp-net-mvc

    See any blog regarding how to return a file.  Unfortunately, you have not shared any code so we have no idea what you're doing.

    Tuesday, October 27, 2020 6:14 PM
  • User270757467 posted

    Sorry, forgot the paste the code.

     

    Partial Class OpenDoc

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim DocName As String = Request.QueryString("DocName")

    'check if file exists if not upload it

    loadfile(DocName)

    'open the file

    ForceDownload(DocName)

     

    'Response.Redirect(System.Configuration.ConfigurationManager.AppSettings("XLSPathUrl") & DocName)

    End Sub

    ''' <summary>

    ''' this is to make sure the open/save/cancel dialog box comes up

    ''' if you just do a redirect, the file opens in the browser.

    ''' </summary>

    ''' <param name="DocName"></param>

    ''' <remarks></remarks>

    Private Sub ForceDownload(ByVal DocName As String)

    Dim fullPath As String = System.Configuration.ConfigurationManager.AppSettings("XLSPath") & DocName

    Dim MyFile As New FileInfo(fullPath)

    Response.Clear()

    Response.AddHeader("Content-Disposition", "inline;attachment; filename=" & DocName)

    'Response.AddHeader("Content-Length", MyFile.Length.ToString())

    Response.ContentType = "application/octet-stream"

    Response.TransmitFile(MyFile.FullName)

    Response.End()

     

    End Sub

    Public Function loadfile(ByVal docname As String) As Boolean

    'Check if file exists

    If File.Exists(System.Configuration.ConfigurationManager.AppSettings("XLSPath") & docname) = False Then

    Dim myTable As Data.DataTable = LASPBS_Classes.dbCalls.DBbySQL("select doc_blob from laspbs_general_docs where doc_name = '" & docname & "'")

    Dim binary(myTable.Rows(0).Item(0).ToString.Length) As Byte

    Dim filename As String = LASPBS_Classes.LASPBS_Master.GetStateVariable(Context, "UserId").ToString & Format(Now, "yyyyMMddHHmmss").ToString

    binary = myTable.Rows(0).Item(0)

    Dim ms As New System.IO.MemoryStream(binary, 0, binary.Length)

    ms.Write(binary, 0, binary.Length)

    ms.Close()

    Response.Clear()

    Response.Buffer = True

    Response.ContentType = "application/octet-stream"

    Dim filepath As String = System.Configuration.ConfigurationManager.AppSettings("XLSPath") & docname

    Dim fs1 As FileStream = New FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write)

    fs1.Write(binary, 0, binary.Length)

    fs1.Close()

    End If

    End Function

    End Class

    crocboy25

    I have a web page that supplies a link to a PDF file we have on our web server. I pass the ID to the web page and it opens the PDF. This works fine on IE but in Chrome and Edge, it opens up the actual code file in Visual studio. How can I correct this?

    The symptom indicates a missing or incorrect content-type. 

    application/pdf

    https://stackoverflow.com/questions/41386977/return-file-equivalent-in-web-forms

    https://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-asp-net-mvc

    See any blog regarding how to return a file.  Unfortunately, you have not shared any code so we have no idea what you're doing.

    Tuesday, October 27, 2020 7:05 PM
  • User475983607 posted

    Right, you're telling the browser to expect a binary stream.   The stream can be anything and the browser does it's best to pick a viewer.  

    Response.ContentType = "application/octet-stream"

    For the second time, return application/pdf so the browser knows what application opens the file.

    Response.ContentType = "application/pdf"

    Tuesday, October 27, 2020 7:58 PM
  • User270757467 posted

    I saw your "application/pdf" quote from above. I was simply adding the code as you requested, I was going to try it, give me time to do so.

    Wednesday, October 28, 2020 12:06 PM
  • User270757467 posted

    That worked! Thank you.

    Wednesday, October 28, 2020 5:35 PM
  • User270757467 posted

    This actually fixed the problem with PDF but messed up our Excel links. We don't know what type of file maybe opened with the link. It maybe a spreadsheet, a word document or PDF. I guess that's why the Octet stream was used before.  We cant assume it is going to be a PDF.

    Thursday, October 29, 2020 12:26 PM
  • User475983607 posted

    This actually fixed the problem with PDF but messed up our Excel links. We don't know what type of file maybe opened with the link. It maybe a spreadsheet, a word document or PDF. I guess that's why the Octet stream was used before.  We cant assume it is going to be a PDF.

    You'll need to save the file type or read the file extension and return a content-type that matches the file type.  

    Thursday, October 29, 2020 12:42 PM