Web窗体中的.NET MVC FileResult等价物

时间:2010-11-03 15:57:24

标签: asp.net asp.net-mvc

我正在使用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;
    }

2 个答案:

答案 0 :(得分:5)

在ASP.NET Webforms中,您需要手动将文件写入Response流。 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();

此代码未经过测试,但这可以帮助您完成大方向。

答案 1 :(得分:4)

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