.NET MVC FileResult相当于Web窗体 [英] .NET MVC FileResult equivalent in Web Forms

查看:167
本文介绍了.NET MVC FileResult相当于Web窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我要在Web窗体使用什么返回类型?

感谢

 公共FileResult PrintPDFVoucher(对象发件人,EventArgs的发送)
    {
        PdfDocument outputDoc =新PdfDocument();
        PdfDocument pdfDoc = PdfReader.Open(
            使用Server.Mappath(ConfigurationManager.AppSettings [模板])
            PdfDocumentOpenMode.Import
        );        MemoryStream的内存=新的MemoryStream();        尝试
        {
            //添加页面导入文件
            INT = PAGECOUNT pdfDoc.PageCount;
            的for(int i = 0; I<页页次,我++)
            {
                PdfPage页= pdfDoc.Pages [I]
                outputDoc.AddPage(页);
            }
            //目标specifix页
            PdfPage pdfPage = outputDoc.Pages [0];            XGraphics gfxs = XGraphics.FromPdfPage(pdfPage);
            XFont bodyFont =新XFont(Arial字体,10,XFontStyle.Regular);
            //保存
            outputDoc.Save(存储器,真);
            gfxs.Dispose();
            pdfPage.Close();
        }
        最后
        {
            outputDoc.Close();
            outputDoc.Dispose();
        }        VAR的结果=新FileContentResult(memory.GetBuffer(),文本/ PDF);
        result.FileDownloadName =file.pdf;
        返回结果;
    }


解决方案

在ASP.NET Web表单,你需要手动将文件写入响应流。有一个在web表单没有结果的抽象。

  Response.ContentType =应用程序/ PDF
  //直接写入生成的文件响应流
  Response.BinaryWrite(内存); // Response.WriteFile(文件路径);如果你有一个物理文件,你想他们下载
  到Response.End();

这code未经过测试,但这应该让你在大方向。

I'm using FileResult as a return value for a function in MVC that returns a PDF file.

What return type should I use in Web Forms?

Thanks

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

解决方案

In ASP.NET Webforms you'll need to write the file to the Response stream manually. There is no result abstraction in 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();

This code is not tested, but this should get you in the general direction.

这篇关于.NET MVC FileResult相当于Web窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆