简体   繁体   English

代码在控制台应用程序上有效,但在ASP.NET应用程序上无效

[英]Code works on console application but not on ASP.NET application

I want to create reports in my ASP.NET MVC 5 application. 我想在我的ASP.NET MVC 5应用程序中创建报告。 I found this code (Hello world example in http://report.sourceforge.net/ ): 我找到了以下代码( http://report.sourceforge.net/中的 Hello world示例):

    Report report = new Report(new PdfFormatter());
    FontDef fd = new FontDef(report, "Helvetica");
    FontProp fp = new FontPropMM(fd, 25);
    Page page = new Page(report);
    page.AddCB_MM(80, new RepString(fp, "Hello World!"));
    RT.ViewPDF(report, "HelloWorld.pdf");

When I put this code inside a main in a console application, it creates and opens a PDF file that writes "Hello World". 当我将此代码放入控制台应用程序的主体中时,它将创建并打开一个写有“ Hello World”的PDF文件。 But when I put the same code inside a controller in ASP.NET like this, I get nothing, no PDF opens or no PDF is saved in the server (I click mywebsite/Person/Print) : 但是,当我将相同的代码放入ASP.NET的控制器中时,却什么也没得到,没有打开PDF或没有将PDF保存在服务器中(我单击mywebsite / Person / Print):

 public ActionResult Print(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Person person= db.Person.Find(id);
        if (person== null)
        {

            Report report = new Report(new PdfFormatter());
            FontDef fd = new FontDef(report, "Helvetica");
            FontProp fp = new FontPropMM(fd, 25);
            Page page = new Page(report);
            page.AddCB_MM(80, new RepString(fp, "Hello World!"));
            RT.ViewPDF(report, "HelloWorld.pdf");
        }
        return View(person);
    }

How can I modify my ASP.NET application to let user get PDFs using this code? 如何修改我的ASP.NET应用程序以使用户使用此代码获取PDF? (I will also appreciate if you know any cool-free reporting tools for ASP.NET MVC 5.) Thanks. (如果您知道ASP.NET MVC 5的任何免费报告工具,我也将不胜感激。)谢谢。

Not sure what libraries you are using there but the general flow of your action should be like: 不确定在那里使用的是什么库,但是操作的一般流程应该是这样的:

  • prepare report object 准备报告对象
  • obtain byte array representing the content of the PDF file 获取表示PDF文件内容的字节数组
  • return a File from your action using the File method: Controller.File 使用File方法从您的操作返回文件: Controller.File

The result on the client would be a file download prompt 客户端上的结果将是文件下载提示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM