0

I am trying to return a file (.jpg) that is stored inside the /images folder of my ASP MVC application.

For some reason, the file downloaded is broken/corrupted and none of the photo viewers are recognising the file. I'm certain that the correct file is reachable as the file size is exactly the same and I have specified the contentType in the File() function.

Please can someone help me out? Thanks.

public ActionResult order(int? id)
{
   // Logic to get the file path and file name from database
   // var ImageName = "file" 
   // var filepath = will be something like ~/images/file.jpg

   byte[] filedata = System.IO.File.ReadAllBytes(filepath);
   string contentType = MimeMapping.GetMimeMapping(filepath);
   return File(filepath, contentType, ImageName+".jpg");
}
Vincent
  • 3
  • 4
  • 1
    Take a look at this post [5826649](http://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-asp-net-mvc) – Felix Cen Jan 14 '16 at 21:14

1 Answers1

0

I think you have a file path problem since the root path in aspnet applications are not the same as the physical path

try adding Server.MapPath

System.IO.File.ReadAllBytes(Server.MapPath(@"~/images/file.jpg")); 
TerribleDev
  • 2,195
  • 1
  • 19
  • 31