生成报告时,不能像方法一样使用不可调用的成员“文件”
我在之前的项目中使用了一种生成报告的方法,并尝试在新项目中使用相同的方法。 但是,我遇到“非invocable成员’文件’不能像方法一样使用”错误和文件无法识别。 通过查看文件的引用,它似乎是上一个项目中的FileContentResult Controller.File()
,但是新项目中的System.IO.File()
(即使我使用System.IO删除;来自引用行,我遇到“名称’文件’在当前上下文中不存在”错误)。 有什么想法解决这个问题吗?
public static FileResult GenerateReport() { EmployeeTableAdapter ta = new EmployeeTableAdapter(); EmployeeDS ds = new EmployeeDS(); ds.Employee.Clear(); ds.EnforceConstraints = false; ta.Fill(ds.Employee); ReportDataSource rds = new ReportDataSource(); rds.Name = "ReportingDataSet"; rds.Value = ds.Employee; ReportViewer rv = new Microsoft.Reporting.WebForms.ReportViewer(); rv.ProcessingMode = ProcessingMode.Local; rv.LocalReport.ReportPath = System.Web.HttpContext.Current. Server.MapPath("~/Reporting/Employee.rdlc"); rv.LocalReport.EnableHyperlinks = true; rv.LocalReport.EnableExternalImages = true; // Add the new report datasource to the report. rv.LocalReport.DataSources.Add(rds); rv.LocalReport.EnableHyperlinks = true; rv.LocalReport.Refresh(); byte[] streamBytes = null; string mimeType = ""; string encoding = ""; string filenameExtension = ""; string[] streamids = null; Warning[] warnings = null; streamBytes = rv.LocalReport.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings); return File(streamBytes, mimeType, "Application" + "_" + ".pdf"); }
注意:我使用MVC5和SQL Server。
System.IO.File
是一个用于读/写文件的静态助手类,你不能创建一个静态类的实例,所以你当然不能返回它。
您正在寻找的File
是System.Web.MVC
命名空间中的一个方法,它返回一个FileContentResult
,请参见msdn 。
将方法移动到Controller解决了问题。 非常感谢@AlexanderDerck的帮助。
您可能希望将控制器的实例传递给您的方法
在你的控制器方法中
public async Task GetFile() { ... return await _fileManager.GetFileAsync(this, "filename", mimeType); }
在你的其他类FileManager
public async Task GetFileAsync(ControllerBase controller, string filename, string mimeType) { ... return controller.File(memory, mimeType, filename); }
- 在ASP.NET Web应用程序中创建线程的正确方法
- 如何在ScriptManager中更改已注册脚本的顺序
- 文件夹浏览器列出WPF中的所有系统驱动器
- md5哈希是否一致或服务器依赖的结果?
- 在ASP.NET(C#)中实现安全,独特的“一次性”激活URL
- 在Asp.net(c#)中,如何在数据库中存储图像(linq to sql)?
- 如何基于Session数据在ASP.NET MVC中实现授权检查?
- 基于当前用户的“角色”动态构建ASP.NET MVC主页面菜单
- Oracle .Net ManagedDataAccess错误:无法从程序集加载类型’OracleInternal.Common.ConfigBaseClass’