ASP.NET MVC3: loading an image through a controller

I tried using the answer here , but that 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 the page, I get the error message "I can not access the closed file." I made a mistake in the error, but I found a download related error. What causes the problem here?

+7
source share
1 answer

Try it like this:

 public ActionResult ShowImage() { var file = Server.MapPath("~/App_Data/UserUpload/asd.png"); return File(file, "image/png", Path.GetFileName(file)); } 

or if you want to create a separate file name:

 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); } 
+9
source

All Articles