【问题标题】:.NET MVC FileResult equivalent in Web FormsWeb 窗体中的 .NET MVC FileResult 等效项
【发布时间】:2010-11-03 15:57:24
【问题描述】:

我使用 FileResult 作为 MVC 中返回 PDF 文件的函数的返回值。

我应该在 Web 表单中使用什么返回类型?

谢谢

public FileResult PrintPDFVoucher(object sender, EventArgs e)
    {
        PdfDocument outputDoc = new PdfDocument();
        PdfDocument pdfDoc = PdfReader.Open(
            Server.MapPath(ConfigurationManager.AppSettings["Template"]),
            PdfDocumentOpenMode.Import
        );

        MemoryStream memory = new MemoryStream();

        try
        {
            //Add pages to the import document
            int pageCount = pdfDoc.PageCount;
            for (int i = 0; i < pageCount; i++)
            {
                PdfPage page = pdfDoc.Pages[i];
                outputDoc.AddPage(page);
            }
            //Target specifix page
            PdfPage pdfPage = outputDoc.Pages[0];

            XGraphics gfxs = XGraphics.FromPdfPage(pdfPage);
            XFont bodyFont = new XFont("Arial", 10, XFontStyle.Regular);


            //Save 
            outputDoc.Save(memory, true);
            gfxs.Dispose();
            pdfPage.Close();
        }
        finally
        {
            outputDoc.Close();
            outputDoc.Dispose();
        }

        var result = new FileContentResult(memory.GetBuffer(), "text/pdf");
        result.FileDownloadName = "file.pdf";
        return result;
    }

【问题讨论】:

    标签: asp.net asp.net-mvc


    【解决方案1】:

    在 ASP.NET Webforms 中,您需要手动将文件写入响应流。网络表单中没有结果抽象。

      Response.ContentType = "Application/pdf";      
      //Write the generated file directly to the response stream
      Response.BinaryWrite(memory);//Response.WriteFile(FilePath); if you have a physical file you want them to download
      Response.End();
    

    此代码未经测试,但这应该可以帮助您了解大体方向。

    【讨论】:

      【解决方案2】:

      经典的 ASP.NET 没有返回类型的概念。解决这个问题的方法是创建一个自定义 .ashx 页面/处理程序来提供文件。

      此文件的代码应类似于:

      public class Download : IHttpHandler 
      {
          public void ProcessRequest (HttpContext context) 
          {
              PdfDocument outputDoc = new PdfDocument();
              PdfDocument pdfDoc = PdfReader.Open(
                  Server.MapPath(ConfigurationManager.AppSettings["Template"]),
                  PdfDocumentOpenMode.Import
              );
      
              MemoryStream memory = new MemoryStream();
      
              try
              {
                  //Add pages to the import document
                  int pageCount = pdfDoc.PageCount;
                  for (int i = 0; i < pageCount; i++)
                  {
                      PdfPage page = pdfDoc.Pages[i];
                      outputDoc.AddPage(page);
                  }
                  //Target specifix page
                  PdfPage pdfPage = outputDoc.Pages[0];
      
                  XGraphics gfxs = XGraphics.FromPdfPage(pdfPage);
                  XFont bodyFont = new XFont("Arial", 10, XFontStyle.Regular);
      
      
                  //Save 
                  Response.ContentType = ""text/pdf"";
                  Response.AppendHeader("Content-Disposition","attachment; filename=File.pdf");
      
                  outputDoc.Save(Response.OutputStream, true);
      
                  gfxs.Dispose();
                  pdfPage.Close();
              }
              finally
              {
                  outputDoc.Close();
                  outputDoc.Dispose();
              }
          }
      
          public bool IsReusable
          {
              get 
              {
                     return false;
              }
          }
      }
      

      【讨论】:

      • 感谢“内容处置”标题
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 1970-01-01
      • 2010-09-20
      相关资源
      最近更新 更多