【发布时间】:2019-02-12 18:05:36
【问题描述】:
我有两种下载文件方法,所以我想将实际命中磁盘的部分提取到某个帮助程序/服务类,但我很难将该文件返回给控制器,然后返回给用户
我如何从不是从 Controller 派生的类返回具有来自 Mvc.ControllerBase.File 的易于使用的方法的文件?
public (bool Success, string ErrorMessage, IActionResult File) TryDownloadFile(string FilePath, string FriendlyName)
{
try
{
var bytes = File.ReadAllBytes(FilePath);
if (FilePath.EndsWith(".pdf"))
{
return (true, "", new FileContentResult(bytes, "application/pdf"));
}
else
{
return (true, "", ControllerBase.File(bytes, "application/octet-stream", FriendlyName));
}
}
catch (Exception ex)
{
return (false, ex.Message, null);
}
}
错误是
非静态字段、方法或属性“ControllerBase.File(Stream, string, string)”需要对象引用
对于这一行:
return (true, "", ControllerBase.File(bytes, "application/octet-stream", FriendlyName));
有没有可能做到这一点?
【问题讨论】:
标签: c# asp.net-core asp.net-core-mvc