加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-15 23:55:42 所属栏目:asp.Net 来源:网络整理
导读:我正在使用FileResult作为MVC中返回PDF文件的函数的返回值. 我应该在Web窗体中使用什么返回类型? 谢谢 public FileResult PrintPDFVoucher(object sender,EventArgs e) { PdfDocument outputDoc = new PdfDocument(); PdfDocument pdfDoc = PdfReader.Open(
我正在使用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 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();

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读