Send RadFlowDocument as PDF to user

半世苍凉 提交于 2019-12-12 02:39:50

问题


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?


回答1:


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)



来源:https://stackoverflow.com/questions/35000500/send-radflowdocument-as-pdf-to-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!