2012-07-19 27 views
0

我試圖加載硬盤中的圖像列表。但圖像不顯示。無法加載MVC3中的外部圖像列表

.cshtml

@foreach (var item in Model) 
      { 

    <img src="@item.FilePath" class="image0" style="width:100px; height:100px" alt="" /> 

      } 

控制器:

List<PageModel> filePath = documentManager.GetPageModels(DocId); 
      return View(filePath); 

型號:

public List<PageModel> GetPageModels(int documentId) 
     { 
      List<PageModel> modelList = new List<PageModel>(); 
      foreach (var page in db.Pages.Where(model=>model.DocumentId == documentId).ToList()) 
      { 
       PageModel model = new PageModel(); 

       model.FilePath = page.FilePath; 

       modelList.Add(model); 
      } 
      return modelList; 
     } 

有人可以幫我解決這個問題?

+0

外部圖像?你正在建立一個網絡應用程序?然後,圖像的URL必須是http:// - 某事,而不是d:\ - 某事。 – 2012-07-19 12:41:02

+0

沒有兄弟...我需要在我的硬盤驅動器中加載圖像 – Krishan 2012-07-19 12:46:44

+0

這很好,但因爲它是您的瀏覽器加載文件,他們需要一個適合文件位置的Url。http:// - something for圖片關閉網站,或放在下面的答案,文件://- 本地文件的東西。 – 2012-07-20 08:17:42

回答

1

你真的想加載你的本地文件嗎?這只是一個網站,是的,因爲沒有其他人能夠看到它們。

如果這是一個瀏覽器需要以下打開本地文件的情況下:

file:///C|/somepath/mylocalfile.html 

這裏的更多詳細信息:Django: how to open local html files directly in the browser with links like href="file:///C:/path/file.html"

更詳細然後給我原來的答覆(其中上述整理評論):

@foreach (var item in Model) 
{ 
    <img src="@Html.ConvertLocalFilePathToBrowserPath(item.FilePath)" class="image0" style="width:100px; height:100px" alt="" /> 
} 

然後你會在某個地方靜態類的幫助:

public static class MyHtmlHelpers 
{ 
    public static string ConvertLocalFilePathToBrowserPath(this HtmlHelper htmlHelper, string filePath) 
    { 
     return string.Format("file:///{0}", filePath.Replace(Path.PathSeparator.ToString(CultureInfo.InvariantCulture), "/")); 
    } 
} 
+0

Steve,Image URL取自數據庫,我使用的是asp.net MVC3。我需要一個答案使用ASP.net MVC3。我是MVC的新手,我只是在學習MVC。 – Krishan 2012-07-19 13:45:34