不可调用成员 'File' 在生成报告时不能像方法一样使用
Non-invocable member 'File' cannot be used like a method while generating Reports
我在以前的项目中使用了一种生成报告的方法,并尝试在新项目中使用相同的方法。但是,我遇到"Non-invocable member 'File' cannot be used like a method"错误,无法识别文件。通过查看文件的引用,它似乎是 FileContentResult Controller.File()
在以前的项目中,但在新项目中是 System.IO.File()
(即使我从引用行中删除了 using System.IO; ,我遇到 "The name 'File' does not exist in the current context" 错误)。有解决问题的想法吗?
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服务器。
System.IO.File
是 reading/writing 文件的静态助手 class,你不能创建静态 class 的实例,所以你当然不能 return它。
您要查找的 File
是 System.Web.MVC
命名空间中的一个方法,其中 return 是 FileContentResult
,请参阅 here on msdn。
将方法移至 Controller 解决了问题。非常感谢@AlexanderDerck 的帮助。
您可能希望像这样将控制器实例传递给您的方法
在你的控制器方法中
public async Task<IActionResult> GetFile()
{
...
return await _fileManager.GetFileAsync(this, "filename", mimeType);
}
在你的另一个classFileManager
public async Task<FileStreamResult> GetFileAsync(ControllerBase controller, string filename, string mimeType)
{
...
return controller.File(memory, mimeType, filename);
}
我在以前的项目中使用了一种生成报告的方法,并尝试在新项目中使用相同的方法。但是,我遇到"Non-invocable member 'File' cannot be used like a method"错误,无法识别文件。通过查看文件的引用,它似乎是 FileContentResult Controller.File()
在以前的项目中,但在新项目中是 System.IO.File()
(即使我从引用行中删除了 using System.IO; ,我遇到 "The name 'File' does not exist in the current context" 错误)。有解决问题的想法吗?
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服务器。
System.IO.File
是 reading/writing 文件的静态助手 class,你不能创建静态 class 的实例,所以你当然不能 return它。
您要查找的 File
是 System.Web.MVC
命名空间中的一个方法,其中 return 是 FileContentResult
,请参阅 here on msdn。
将方法移至 Controller 解决了问题。非常感谢@AlexanderDerck 的帮助。
您可能希望像这样将控制器实例传递给您的方法
在你的控制器方法中
public async Task<IActionResult> GetFile()
{
...
return await _fileManager.GetFileAsync(this, "filename", mimeType);
}
在你的另一个classFileManager
public async Task<FileStreamResult> GetFileAsync(ControllerBase controller, string filename, string mimeType)
{
...
return controller.File(memory, mimeType, filename);
}