我尝试使用here中的答案,但是没有用。我有以下代码:
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;
}
}
打开页面时,出现错误消息:“无法访问关闭的文件。”。我对该错误进行了一些谷歌搜索,但我仅发现此错误与上传有关。是什么导致这里的问题?
最佳答案
尝试这样:
public ActionResult ShowImage()
{
var file = Server.MapPath("~/App_Data/UserUpload/asd.png");
return File(file, "image/png", Path.GetFileName(file));
}
或者,如果您需要单独的文件名:
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);
}
关于image - ASP.NET MVC3:通过 Controller 加载图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6225485/