Displaying PDF throws error when Ajax on page

4 posts, 0 answers
  1. David Penny
    David Penny avatar
    174 posts
    Member since:
    Jun 2006

    Posted 02 Sep 2010 Link to this post

    Hi,

    I have a page running a generated Crystal Report which is producing a PDF file.  I then display this using the following code:

    Using client As System.Net.WebClient = New System.Net.WebClient()
        HttpContext.Current.Response.ContentType = "application/pdf"
        HttpContext.Current.Response.WriteFile(FileName)
        HttpContext.Current.Response.Flush()
        HttpContext.Current.Response.Close()
        HttpContext.Current.Response.End()
    End Using

    This works fine when the page does not use Ajax, but as soon as I add Ajax to it an error gets thrown.  I would like to Ajaxify the page so I can show a progress spinner as the report is generated, rather than leave a few seconds when it looks like nothing is happening.

    If I remove Ajax from the page thie code works fine.

    Add Ajax and it stop working.

    Is there an alternative, or better method of showing the PDF (I also show a Word or Excel document, depending on the selection the user has made)

    Dave Penny
  2. Pavlina
    Admin
    Pavlina avatar
    6187 posts

    Posted 02 Sep 2010 Link to this post

    Hello David,

    Possible reason for this behavior is that your export button is ajaxified. Please examine the following link:
    Export from ajaxified grid

    If this is not the case, please provide a sample code so I can examine your approach.

    Greetings,
    Pavlina
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Krina
    Krina avatar
    2 posts
    Member since:
    Apr 2019

    Posted 05 Apr in reply to Pavlina Link to this post

    Getting problem in Response.write some parser error thrown by server

     

    Public Shared Function DownloadFile(fpname As String, fname As String, forceDownload As Boolean)
                'Dim fullpath As String = fname
                'Dim name As String = Path.GetFileName(fullpath)
                'Dim ext As String = Path.GetExtension(fullpath)

                Dim filepname As String = "C:\Downloads\"
                Dim filename As String = "C:\Downloads\Grid.xlsx"
                Dim result As String
                Dim ext As String
                result = Path.GetFileName(filename)
                ext = Path.GetExtension(filename)


                Dim type As String = ""

                Select Case (ext)

                    Case ".htm"
                    Case ".html"
                        type = "text/HTML"
                    Case ".txt"
                        type = "text/plain"
                    Case ".doc"
                    Case ".rtf"
                        type = "Application/msword"
                    Case ".xls"
                    Case ".csv"
                    Case ".xlsx"
                        type = "Application/x-msexcel"
                    Case ".pdf"
                        type = "application/pdf"
                    Case Else
                        type = "text/plain"
                End Select

                If ((forceDownload)) Then
                    HttpContext.Current.Response.AppendHeader("content-disposition", "attachment; filename=" + filename)
                End If

                If (Not String.IsNullOrEmpty(type)) Then
                    HttpContext.Current.Response.ContentType = type
                End If
                HttpContext.Current.Response.WriteFile(filename)
                HttpContext.Current.Response.End()
                Return ""
            End Function

    Pavlina said:Hello David,

    Possible reason for this behavior is that your export button is ajaxified. Please examine the following link:
    Export from ajaxified grid

    If this is not the case, please provide a sample code so I can examine your approach.

    Greetings,
    Pavlina
    the Telerik team

     
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
     
    Pavlina said:Hello David,

    Possible reason for this behavior is that your export button is ajaxified. Please examine the following link:
    Export from ajaxified grid

    If this is not the case, please provide a sample code so I can examine your approach.

    Greetings,
    Pavlina
    the Telerik team
     
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
     
  4. Peter Milchev
    Admin
    Peter Milchev avatar
    613 posts

    Posted 10 Apr Link to this post

    Hello Krina,

    When downloading a file, the Response needs to be cleared and populated with the file data which will be returned to the client. 

    When an AJAX request is triggered, it expects a given format of the response and that is why the download does not work in this scenario. 

    The solution is to ensure that the postback that is triggered when a file should be downloaded is a full one, with AJAX disabled. You can, for example, disable AJAX via the client-side OnRequestStart Event based on the control that triggers the postback.

    Also, keep in mind that the content-type is not the same for the xlsx, csv and xls file types: https://filext.com/faq/office_mime_types.html

    If the issue persists, please provide a simple runnable project with dummy data and the same AJAX configuration that replicates the issue and attach it to an official support ticket. That would allow us to investigate locally and help you more efficiently. 

    Once we have a solution in the support ticket, we can share it here for convenience and better visibility from the community.

    Regards,
    Peter Milchev
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top