【问题标题】:Call a webapi control to return pdf, but with headers调用 webapi 控件以返回 pdf,但带有标题
【发布时间】:2016-05-16 13:41:07
【问题描述】:

How to retrieve a PDF using WebBrowser or WebClient in .NET?

我有与上述类似的请求,但我希望这是下载窗口或自动下载文件并显示在浏览器底部。

我需要它能够在请求中发送标头(自制/读取标头)。如何重定向到一个页面,该页面将下载文件而不是自动保存?

SetupWebClient 是我设置我需要的标头....

WebClient wc = new WebClient();
wc = this.SetupWebClient(wc);
wc.DownloadData(http://www.testurl.com/PdfController/GetDocumentPDF/" + customerdocument.DocumentId + "/" + customerdocument.CustomerId);

【问题讨论】:

    标签: asp.net-web-api webclient


    【解决方案1】:

    这是我必须做的,如果其他人遇到这种情况....不得不劫持流,并执行以下操作:

    .....
    Stream pdfFileStream = wc.OpenRead(url)
    {
        //Override stream
        System.Web.HttpContext.Current.Response.Clear();
        System.Web.HttpContext.Current.Response.ContentType = "application/pdf";
        System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=bylaws.pdf");
        pdfFileStream.CopyTo(System.Web.HttpContext.Current.Response.OutputStream);
        System.Web.HttpContext.Current.Response.Flush();
        System.Web.HttpContext.Current.Response.SuppressContent = true;
        System.Web.HttpContext.Current.Response.OutputStream.Flush();
        System.Web.HttpContext.Current.Response.OutputStream.Close();
        System.Web.HttpContext.Current.Response.End();
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-24
      • 2017-05-09
      • 2019-01-10
      • 1970-01-01
      • 2012-10-27
      • 2020-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多