user4103496
user4103496

Reputation:

Send RadFlowDocument as PDF to user

I'm using VS 2013, VB.Net, and Telerik 2016 Q1 in an MVC application. I'm using the Telerik RadFlowDocument to create a report, and send it to the user as a pdf. I have successfully created the report and saved it locally. My issue is when I try to convert it to a memory stream and send it back to the user, the user is never given the option to download the report.

I have a button that starts the process via ajax:

function supervisorPDF() {
    $.ajax({
        url: '@Url.Action("GenerateSupervisorReportPDF", "InteriorReport")',
        type: 'POST'
    })
    .success(function (result) {

    })
    .error(function (xhr, status) {
        alert(status);
    })
}

I have a sub routine that receives the ajax call and then generates the report:

    Sub GenerateSupervisorReportPDF()
        Dim generator As New PdfGenerator
        Dim selectedSupervisorName = System.Web.HttpContext.Current.Session("selectedSupervisorName").ToString()
        Dim selectedSupervisorIdNumber = System.Web.HttpContext.Current.Session("selectedSupervisorIdNumber").ToString()
        Dim report As New RadFlowDocument

        Dim viewModel As New SupervisorReportViewModel
        Dim model = viewModel.getSupervisorReportInfo(selectedSupervisorName, selectedSupervisorIdNumber)

        report = generator.generateSupervisorPdf(model)

        Dim provider As New PdfFormatProvider()
        Dim output As New MemoryStream
        provider.Export(report, output)

        Dim buffer As Byte()

        Try
            buffer = output.ToArray
            Response.ClearContent()
            Response.ClearHeaders()
            Response.ContentType = "application/pdf"
            Response.OutputStream.Write(buffer, 0, buffer.Length)
            Response.AddHeader("Content-Disposition", "attachment;filename=SupervisorReport.pdf")
            Response.End()
        Catch ex As Exception

        End Try

    End Sub

In this subroutine, if I declare output as a local file path everything works as intended. However when using this implementation my Response.OutputStream always ends up null. No errors are thrown.

What do I need to change to send this report back to the user in such a way that they have the option of downloading the report?

Upvotes: 1

Views: 469

Answers (1)

fdomn-m
fdomn-m

Reputation: 28611

You won't be able to do this via ajax.

In order for the browser to receive and handle

Content-Disposition:attachment

it must be browsed to directly.

The ajax call may download the binary to jquery, but jquery won't be able to save it locally.

Easiest option is to have the browser open a link to the action that generates the PDF directly, eg:

<a href='@Url.Action("GenerateSupervisorReportPDF", "InteriorReport")'>download report</a>

As an extra, you can reduce all of the Response. code to a simple return FileStreamResult(... and it should handle prompting the user to save etc - I'll leave this to you (as I don't have the exact syntax to hand)

Upvotes: 1

Related Questions