0

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Siemsen
  • 199
  • 1
  • 3
  • 13

1 Answers1

2

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

Community
  • 1
  • 1
Zdeslav Vojkovic
  • 14,391
  • 32
  • 45
  • Could you give me a step-by-step how to do that? – Siemsen Sep 21 '12 at 13:43
  • Are you refering to the WebDev.WebServer40.exe process, because that is under my account. – Siemsen Sep 21 '12 at 14:17
  • It depends on your setup, might be... can you clarify the problem. I initially understood that you can't access the files (user rights issue). do you mean that you can actually access them, but they are not shown in the browser? What is the url show in the `src` attribute of `img` tag. – Zdeslav Vojkovic Sep 21 '12 at 14:28
  • I should be able to access them (local machine, I'm running VS in admin mode). If I type the path I get from the source of the I get the picture, an example of which is "C:\tmp\659\659ce886-8ad2-4842-b41d-c2d4893954b4[0].jpg". But all it shows is the alt text in the browser itself. – Siemsen Sep 21 '12 at 14:39
  • 2
    if you have something like `` then it is wrong, as browser has no idea how to handle an url using protocol it doesn't understand – Zdeslav Vojkovic Sep 21 '12 at 14:57
  • 1
    Ah ok, so I'm guessing I need to create a route or a mapping of some sort right? – Siemsen Sep 21 '12 at 15:00
  • 1
    yes, but in that case you might hit the issue I originally described when you deploy the app to production server. I will also modify my answer to clarify the problem. – Zdeslav Vojkovic Sep 21 '12 at 15:03
  • Could you point me towards some easy guide or tutorial on how to do this? – Siemsen Sep 21 '12 at 15:07
  • 1
    @Siemsen, check this post out for direction on getting the image from an Action method: http://stackoverflow.com/questions/5953045/how-should-i-return-an-image-from-a-controller-action-c-sharp-asp-net-mvc-2 – Gromer Sep 21 '12 at 15:13
  • 1
    The easiest way is to copy the files into a subdir of your web app. Additionally,you would have to transform each file path from something like `c:\bla\myapp\images\x.jpg` to `~/images/x.jpg`. – Zdeslav Vojkovic Sep 21 '12 at 15:14
  • Ok, thanks for the help. I'm marking this as the answer since it solved why this was happening. So thanks alot Zdeslav (and also Gromer for the link) – Siemsen Sep 21 '12 at 15:16