Krishan
Krishan

Reputation: 2487

Can not Load list of external images in MVC3

I'm trying to load list of images in my hard Drive.but Images are not Display.

.cshtml

@foreach (var item in Model)
            {

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

            }  

Controller:

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

Model:

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

Can Someone help me to Solve this Problem?

Upvotes: 0

Views: 451

Answers (1)

Steve Owen
Steve Owen

Reputation: 2071

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), "/"));
    }
}

Upvotes: 1

Related Questions