ASP.NET MVC3:通过控制器加载图像 [英] ASP.NET MVC3: Image loading through controller
本文介绍了ASP.NET MVC3:通过控制器加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用来自此处的答案,但没有奏效.我有以下代码:
I tried using the answer from here, but it did not work. I have the following code:
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;
}
}
当我打开页面时,我收到一条错误消息:无法访问已关闭的文件.".我对错误进行了一些谷歌搜索,但我只发现此错误与上传相关.是什么导致了这里的问题?
When I open up the page I get an error which says: "Cannot access a closed file.". I did some googling on the error, but I only found this error associated with uploading. What causes the issue here?
推荐答案
试试这个:
public ActionResult ShowImage()
{
var file = Server.MapPath("~/App_Data/UserUpload/asd.png");
return File(file, "image/png", Path.GetFileName(file));
}
或者如果你想要一个单独的文件名:
or if you want a separate filename:
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);
}
这篇关于ASP.NET MVC3:通过控制器加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文