首页 新闻 会员 周边

asp.net core PhysicalFileResult导致浏览器地址栏访问图片变成了下载

0
悬赏园豆:5 [已解决问题] 解决于 2018-08-31 12:33

[HttpGet("DownloadFile")]
public IActionResult DownloadFile([FromServices]IHostingEnvironment env, string id)
{
Attachment attachment = AttachmentSystem.GetById(User.Corp.Id, id);
if (attachment == null)
{
return NotFound();
}
string path = Path.Combine(env.ContentRootPath, attachment.SavePath);
if (!System.IO.File.Exists(path))
{
return NotFound();
}
return PhysicalFile(path, MimeMapping.GetMimeMapping(Path.GetExtension(path)), attachment.SourceName + Path.GetExtension(path));
}

用以上方式输出图片,地址栏直接访问会下载图片,怎么修改才能直接在浏览器里显示

小重山的主页 小重山 | 初学一级 | 园豆:173
提问于:2018-08-31 11:42
< >
分享
最佳答案
0

可以使用 FileStreamResult ,下面的代码实测有效

public async Task<IActionResult> Index()
{
    var fs = new FileStream("wwwroot/images/logo.png", FileMode.Open);    
    return new FileStreamResult(fs, "image/png");
}
收获园豆:5
dudu | 高人七级 |园豆:30994 | 2018-08-31 12:04

谢谢,确实可以

小重山 | 园豆:173 (初学一级) | 2018-08-31 12:33
其他回答(2)
0

mark!

he55 | 园豆:303 (菜鸟二级) | 2018-09-12 17:06
0

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

Angkor--:-- | 园豆:1086 (小虾三级) | 2020-06-16 15:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册