0

我正在尝试在我的硬盘中加载图像列表。但图像不显示。

.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;
        }

有人可以帮我解决这个问题吗?

4

1 回答 1

1

You really want to load your local files? This is just a Web site for you, yeah, cos no one else will be able to see them.

If that's the case a browser needs the following to open a local file:

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

Here's more detail: Django: how to open local html files directly in the browser with links like href="file:///C:/path/file.html"

In more detail then given the comment on my original answer (which finished above):

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

And then you would have a helper in a static class somewhere:

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), "/"));
    }
}
于 2012-07-19T13:07:07.147 回答