ASP.NET MVC3:通过控制图像加载 [英] ASP.NET MVC3: Image loading through controller

查看:90
本文介绍了ASP.NET MVC3:通过控制图像加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用答案从这里 ,但没有奏效。我有以下的code:

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;
    }

}

当我打开网页,我得到它说的错误:无法访​​问已关闭的文件。我做了错误的一些使用Google,但我只发现上传有关这个错误。这里是什么导致了这个问题?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆