This is a migrated thread and some comments may be shown as answers.
Displaying PDF throws error when Ajax on page
3 Answers 13 Views
This is a migrated thread and some comments may be shown as answers.
David Penny
Top achievements
Rank 2
David Penny asked on 02 Sep 2010, 11:20 AM
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

3 Answers, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 02 Sep 2010, 12:04 PM
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
0
Krina
Top achievements
Rank 1
answered on 05 Apr 2019, 03:25 PM

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[quote]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
 
[/quote][quote]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
 
[/quote]
0
Peter Milchev
Telerik team
answered on 10 Apr 2019, 07:57 AM
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.
Tags
Ajax
Asked by
David Penny
Top achievements
Rank 2
Answers by
Pavlina
Telerik team
Krina
Top achievements
Rank 1
Peter Milchev
Telerik team
Share this question
or