将RadFlowDocument以PDF格式发送给用户 [英] Send RadFlowDocument as PDF to user

查看:82
本文介绍了将RadFlowDocument以PDF格式发送给用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC应用程序中使用VS 2013,VB.Net和Telerik 2016 Q1.我正在使用Telerik RadFlowDocument 创建报告,并将其以pdf格式发送给用户.我已经成功创建了报告并将其保存在本地.我的问题是,当我尝试将其转换为内存流并将其发送回用户时,从未向用户提供下载报告的选项.

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.

我有一个通过ajax启动该过程的按钮:

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);
    })
}

我有一个子例程,该子例程接收ajax调用,然后生成报告:

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

在此子例程中,如果我将 output 声明为本地文件路径,则一切都会按预期进行.但是,当使用此实现时,我的 Response.OutputStream 总是以null结尾.没有引发任何错误.

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?

推荐答案

您将无法通过Ajax完成此操作.

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

为了使浏览器能够接收和处理

In order for the browser to receive and handle

Content-Disposition:attachment

必须直接浏览到.

ajax调用可能会将二进制文件下载到jquery,但是jquery无法将其保存在本地.

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

最简单的选择是让浏览器打开直接生成PDF的操作的链接,例如:

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>


另外,您可以将所有 Response.代码减少为一个简单的 return FileStreamResult(... ),并且应该处理提示用户保存等-我会把这个留给你(因为我没有确切的语法)


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)

这篇关于将RadFlowDocument以PDF格式发送给用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆