Question

I'm having a problem displaying images that are not inside the application folder but rather in C:\tmp\Somename\somepic.jpg I have been searching and trying things out but to no avail.

What I have so far (that I think is related to this problem) is: In controller

public ActionResult Edit(int? id)
{
    if (id.HasValue)
    {
        var model = GetItems(id.Value);

        ViewBag.Images = Directory.GetFiles(WebConfigurationManager.AppSettings["itemPath"] + model.ContentId.ToString().Substring(0, 3) + "\\", model.ContentId + "*.jpg");
        return View(model);
    }

    return View("Notfound", "Home");
}

and in the view

<div class="row">
@foreach (var item in ViewBag.Images)
{
    <img src="@Url.Content(item)" alt="hello"/>


}

Any help would be appreciated.

Was it helpful?

Solution

The problem seems to be caused by using absolute file paths in URLs, e.g.

<img src="c:\tmp\myimage.jpg" />

This won't work for three reasons:

  • URIs use slashes, not backslashes
  • browser in general have no idea how to handle an url using protocol it doesn't understand (although they do support showing the local files when entered in browser for convenience)
  • when the user opens such web page, the browser would navigate to the file on his local machine, not on the web server. Browsers forbid this as it is a huge security hole.

This is also covered in src absolute path problem

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top