Esa*_*Esa 7 controller image asp.net-mvc-3
我尝试使用这里的答案,但它没有用.我有以下代码:
public ActionResult ShowImage()
{
using (FileStream stream = new FileStream(Path.Combine(Server.MapPath("/App_Data/UserUpload/asd.png")), FileMode.Open))
{
FileStreamResult result = new FileStreamResult(stream, "image/png");
result.FileDownloadName = "asd.png";
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
当我打开页面时,我收到一条错误,上面写着:"无法访问已关闭的文件." 我做了一些关于错误的谷歌搜索,但我只发现了与上传相关的错误.是什么导致这个问题?
试试这样:
public ActionResult ShowImage()
{
var file = Server.MapPath("~/App_Data/UserUpload/asd.png");
return File(file, "image/png", Path.GetFileName(file));
}
Run Code Online (Sandbox Code Playgroud)
或者如果你想要一个单独的文件名:
public ActionResult ShowImage()
{
var path = Server.MapPath("~/App_Data/UserUpload");
var file = "asd.png";
var fullPath = Path.Combine(path, file);
return File(fullPath, "image/png", file);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6908 次 |
| 最近记录: |