生成报告时,不能像方法一样使用不可调用的成员“文件”

我在之前的项目中使用了一种生成报告的方法,并尝试在新项目中使用相同的方法。 但是,我遇到“非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是一个用于读/写文件的静态助手类,你不能创建一个静态类的实例,所以你当然不能返回它。

您正在寻找的FileSystem.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); }