2016-01-14 92 views
0

我想返回存儲在我的ASP MVC應用程序的/ images文件夾內的文件(.jpg)。如何在ASP MVC中返回文件?

出於某種原因,下載的文件已損壞/損壞,並且沒有任何照片查看器正在識別該文件。我確定正確的文件是可以訪問的,因爲文件大小完全相同,並且我已經在File()函數中指定了contentType。

請有人可以幫我嗎?謝謝。

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"); 
} 
+1

看看這個[5826649](http://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-asp-net-mvc) –

回答

0

我認爲你有一個文件路徑的問題,因爲在ASPNET應用程序的根路徑是不一樣的物理路徑

嘗試添加使用Server.Mappath

System.IO.File.ReadAllBytes(Server.MapPath(@"~/images/file.jpg")); 
+0

非常感謝:) – Vincent