【问题标题】:C# Non-invocable member 'File' cannot be used like a methodC# 不可调用成员“文件”不能像方法一样使用
【发布时间】:2018-05-29 07:35:05
【问题描述】:
namespace Tema2_PS.Services
{
public class CSVExporter : Exporter
{
   public FileContentResult convert(List<Ticket> ticketList)
   {
       string csv = "";

        return File(new System.Text.UTF8Encoding().GetBytes(csv.ToString()), "text/csv", "Export.csv");
   }
}
}

【问题讨论】:

  • 你肯定是指return new File(...).. 但即便如此File 肯定不能转换为FileContentResult。但是我们不知道这两个类是如何相互依赖的,因此不知道如何进行这种转换。
  • FileController 中的一个方法。您的代码不在控制器中,因此无法编译。因此,您将希望 new 向上 FileContentResult

标签: c# file csv export


【解决方案1】:

您正试图返回类 File 而不是实例。 在不知道 FileContentResult 类的内容的情况下,我只能假设它将文件作为构造函数中的参数。

return new FileContentResult(new File(new System.Text.UTF8Encoding().GetBytes(csv.ToString()), "text/csv", "Export.csv"));

如果不是这样,并且 FileContentresult 继承自 File 类

return new File(new System.Text.UTF8Encoding().GetBytes(csv.ToString()), "text/csv", "Export.csv"))

【讨论】:

  • 我使用 MVC,并且在 TicketController 中有这个方法: public FileContentResult Export(String type, int movieId) { Exporter exporter = ExportFactory.get(type); List ticketList = TicketDAO.getInstance().getTickets(movieId); return exporter.convert(ticketList); }
  • 你有解决这个问题的办法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-10
  • 2013-07-07
  • 1970-01-01
  • 2017-04-11
  • 2016-05-01
  • 2016-04-11
相关资源
最近更新 更多