【问题标题】:MvcRazorToPdf save as MemoryStream or Byte[]MvcRazorToPdf 保存为 MemoryStream 或 Byte[]
【发布时间】:2014-06-09 22:32:38
【问题描述】:

我在 Azure 网站中使用 MvcRazorToPdf 并创建我的 PDF 并将它们输出到浏览器中。

现在我正在创建一个新功能,将 PDF 作为附件直接通过电子邮件发送(而不在浏览器中输出)。

有人知道是否可以将 PDF(使用 MvcRazorToPdf)保存为 MemoryStream 或 Byte[]?

【问题讨论】:

  • 我无法帮助您处理 MVC 部分,但 PdfActionResult.cs 中的最后一行代码包含您正在寻找的 byte 数组和 MemoryStream

标签: asp.net-mvc pdf pdf-generation asp.net-mvc-5 mvcrazortopdf


【解决方案1】:

我认为你可以在 ResultFilter 中处理这个问题,我使用下面的代码来允许用户下载文件并提示下载弹出窗口,这样你就可以获取所有内存流并存储在某处以发送电子邮件后记。

public class ActionDownloadAttribute : ActionFilterAttribute { public override void OnResultExecuted(ResultExecutedContext filterContext) { filterContext.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + "Report.pdf"); base.OnResultExecuted(filterContext); } }

 [ActionDownload]
 public ActionResult GeneratePdf()
 {
       List<Comment> comments = null;

        using (var db = new CandidateEntities())
        {
            comments = db.Comments.ToList();
        }

        return new PdfActionResult("GeneratePdf", comments);
 }

【讨论】:

    【解决方案2】:

    我已经实现了类似的东西。所以基本上我没有改变我输出PDF的方法。我所做的是使用restsharp在我得到PDF的URL上提出请求,然后你所拥有的就是几行(这只是部分代码,所以你可以得到想法)

    var client = new RestClient(IAPIurl);
    var request = new RestRequest(String.Format(IAPIurl_generatePDF, targetID), Method.GET);
    RestResponse response = (RestResponse) client.Execute(request);
    
    // Here is your byte array 
    response.RawBytes
    

    否则,您可以使用我在here 中讨论的直接返回文件的答案。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-07
      • 1970-01-01
      • 2011-09-07
      • 2012-01-27
      • 2012-01-29
      相关资源
      最近更新 更多