首页 > 解决方案 > 如何调用 Export PDF with Reserved.ReportViewerWebControl.axd....来自后端 C# 代码的 URL

问题描述

我的目标是在 C# 端调用由 Rdlc 导出 PDF 按钮给出的 ExportBaseUrl Link ,我想这样做,因为有 285 个报告,每个报告都有差异参数,所以这将花费很多时间。

我已经研究过一种解决方案,但需要 15 分钟才能将 2 页 RDLC 加载到 pdf。由于响应迟到或发生了一些僵局,它需要时间,

这就是我正在做的事情。JS文件

  var reportViewerName = ControlName; //Name attribute of report viewer control.
    var src_url = $find(reportViewerName)._getInternalViewer().ExportUrlBase + 'PDF';

    var contentDisposition = 'AlwaysInline'; //Content Disposition instructs the server to either return the PDF being requested as an attachment or a viewable report.
    var src_new = src_url.replace(/(ContentDisposition=).*?(&)/, '$1' + contentDisposition + '$2');


    window.open("/printPDF.asx?url=" + encodeURIComponent("http://localhost:5402"+src_new));

PrintPDF.aspx 文件是这样的

        using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Action;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace WebApp.WebAPI
{
    /// <summary>
    /// Summary description for printPDF
    /// </summary>
    public class printPDF : IHttpHandler
    {

        public  void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");


            string url = context.Request.QueryString["url"];

            // Download data.
             DownloadFile(url, context.Server.MapPath("~/pdf/pdffile.pdf"), context.Request).Wait();

            PdfDocument pdfDoc = new PdfDocument(new PdfReader(context.Server.MapPath("~/pdf/pdffile.pdf")), new PdfWriter(context.Server.MapPath("~/pdf/pdffileAuto.pdf")));
            // add content
            PdfAction action = PdfAction.CreateJavaScript("this.print({bUI: true, bSilent: true, bShrinkToFit: true});");
            pdfDoc.GetCatalog().SetOpenAction(action);
            pdfDoc.Close();

            context.Response.Clear();
            context.Response.ContentType = "application/pdf";
            context.Response.AddHeader("Content-Disposition",
                "AlwaysInline;filename=\"FileName.pdf\"");

            context.Response.BinaryWrite(File.ReadAllBytes(context.Server.MapPath("~/pdf/pdffileAuto.pdf")));

            context.Response.Flush();

            context.Response.End();

        }
        public async Task DownloadFile(string url, string destinationPath, HttpRequest req)
        {

            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";

            var encoding = new UTF8Encoding();

            request.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-IN");
            request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
            request.Accept = "text/html, application/xhtml+xml, image/jxr, */*";
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko";
            request.KeepAlive = true;
            request.Proxy = null;
            request.CookieContainer = new CookieContainer();
            Uri target = new Uri("http://localhost:5402/");

            foreach (String item in req.Cookies)
            {
                request.CookieContainer.Add(new Cookie(item, req.Cookies[item].Value) { Domain = target.Host });
            }
await request.GetResponseAsync().ContinueWith(t1 =>   
            {
                using (var responseStream = t1.Result.GetResponseStream())
                {


                    if (responseStream == null)
                        return;

                    int bufferSize = 1024;
                    byte[] buffer = new byte[bufferSize];
                    int bytesRead = 0;

                    using (FileStream fileStream = File.Create(destinationPath))
                    {
                        while ((bytesRead = responseStream.Read(buffer, 0, bufferSize)) != 0)
                        {
                            fileStream.Write(buffer, 0, bytesRead);
                        }
                    }
                }
                t1.Result.Close();
            });

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

线 等待 request.GetResponseAsync().ContinueWith(t1 =>

使用异步和不使用异步大约需要 15 分钟一次,第二次进入死锁/冻结

由于 url 引发 500 内部服务器错误,我还必须添加 cookie。

如果我直接在浏览器上调用 url,它会在 1 秒内运行。

因此,如果有人知道什么是问题或可以提供帮助,那将是非常大的帮助。

感谢您提前提供帮助。

好的

我发现了什么问题,

问题是选项卡请求 PrintPDF.aspx 并且该页面请求同一站点上的其他 URL。因此,直到 PrintPDF.aspx 对未调用 (HttpWebRequest) 的选项卡的响应完成。

有什么理由吗?我虽然在 web.config 中设置了 maxconnection

标签: javascriptc#asp.netreportrdlc

解决方案


我必须通过制作 2 个差异文件来修复它,第一个 ASPX 文件显示页面并在线程中生成 pdf,然后在页面加载调用 Ashx 文件检查文件是否生成,如果生成则返回文件。

没有其他办法,所以我通过 2 个链接重定向调用修复了它。

感谢所有帮助过我的人。


推荐阅读