2012-07-19 51 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