5

I need to set an image source to a location on the network.

The image is located at machineName\mappedPath\abc.jpg.

It does not load in any browser though all I need it to work in is IE v9 and above. When I inspect the location in the source it is set to

<img src="\\machineName\mappedPath\abc.jpg">

When i right click on the image placeholder in IE and look at the properties I see the address is set to

file://machineName/mappedPath/abc.jpg

Using file explorer with either of those paths opens the image fine.

I've tried adding IP. Can't add a domain name as its not on a domain.

Other paths I've tried setting the source to directly below. I've read a few places that 5 front slashes are required but it hasn't made any difference

<img src="file://\\machineName\mappedPath\abc.jpg">
<img src="file:////\\machineName\mappedPath\abc.jpg">
<img src="file://///\\machineName\mappedPath\abc.jpg">
<img src="file:////machineName/mappedPath/abc.jpg">
<img src="file://///machineName/mappedPath/abc.jpg">
<img src="\\machineName\mappedPath\abc.jpg">

I've also tried enabling file sharing by adding a firewall rule

https://blogs.msdn.microsoft.com/windows_azure_connect_team_blog/2011/01/20/windows-azure-connect-use-case-enable-file-sharing-on-windows-azure-vm/

On a side note, does the path have to be mapped as a network drive or is it sufficient to set it up as a network share?

Not a definitive source but this is pretty common kind of information I've come across https://jonlabelle.com/snippets/view/html/create-html-link-to-unc-path , but for which won't work for me (in IE)

user48408
  • 3,234
  • 11
  • 39
  • 59

4 Answers4

4

I've found this post. It might have some relevant information.

Here's the answer from Paul Zahra:

FireFox would not display networked images so I created an MVC helper that extends HtmlHelper.

public static class ImageHelper
{
    /// <summary>Converts a photo to a base64 string.</summary>
    /// <param name="html">The extended HtmlHelper.</param>
    /// <param name="fileNameandPath">File path and name.</param>
    /// <returns>Returns a base64 string.</returns>
    public static MvcHtmlString PhotoBase64ImgSrc(this HtmlHelper html, string fileNameandPath)
    {
        var byteArray = File.ReadAllBytes(fileNameandPath);
        var base64 = Convert.ToBase64String(byteArray);

        return MvcHtmlString.Create(String.Format("data:image/gif;base64,{0}", base64));
    }
}

use in the MVC View like so:

using 
<img src="@Html.PhotoBase64ImgSrc(image)" height="60px" width="60px" alt="photo" />

here the 'image' in @Html.PhotoBase64ImgSrc(image) is a pure network UNC, e.g.

//Photos/ebaebbed-92df-4867-afe8-0474ef8644eb.jpg
RubbelDieKatz
  • 1,134
  • 1
  • 15
  • 32
  • 1
    This does work and it will be handy I'm sure for someone, but because I can have up to 5000 images it isn't a solution I can use as it would involve changing the architecture too much to ensure only visible images are being base 64 encoded at any time – user48408 Aug 29 '17 at 08:47
  • Perhaps you could load the images when the user scrolls to them? – RubbelDieKatz Aug 29 '17 at 08:49
  • 1
    I'm using a grid (datatables.net with deferred rendering enabled). It is able to take care of only loading visible images. I could conceivably see this working by changing over to a server side mode but there is quite a bit of side work involved. This is a viable solution but before undertaking I'd prefer to see if I can just somehow set the image src. I'm only concerned about IE, and while there are problems with this approach in chrome and FF, setting the image source like this in IE should be do-able – user48408 Aug 29 '17 at 10:03
4

You should add 3 more / to the path with the file:// prefix:

<img src="file://///machineName/mappedPath/abc.jpg">
yorammi
  • 6,272
  • 1
  • 28
  • 34
  • Worked for me. Also the site URL which hosts this HTML needs to be in the Intranet Security Zone in Windows Internet Settings (IE Settings). It works in IE11 and Edge Chromium. Still struggling to get it to work in Chrome, which errors: `` – Adam Jun 10 '20 at 19:02
2

The first two are part of the protocol indicator ("file://"), then host name (should be the host name which the file should be accessed from. i.e. localhost, which is the default, so can be left blank!), then another slash, then the UNC path starts, which explains the last two slashes, as all UNC paths start with two slashes.

Tried the below?

<img src="file://localhost/\\machineName\mappedPath\abc.jpg">
Syden
  • 8,425
  • 5
  • 26
  • 45
  • I did yes, and as you mentioned the localhost part is optional so file:///\\ and file://localhost/\\ are equivalent. I won't be put up all the combinations of slashes i've tried but i've tried a lot. I'm now thinking its a security issue and it's not possible or it requires some configuration on my part with how i've set up the network share – user48408 Aug 29 '17 at 15:29
  • file:///\\ should work as well. Are you trying to access from outside your firewall? Also have you considered setting an http server as plan b? this would unify your images path syntax. – Syden Aug 29 '17 at 16:03
  • Pictures outside the root directory can't be accessed by file:// via UNC due to security restrictions! – floGalen Sep 04 '17 at 14:39
1

I have some pictures on my NAS. In order to be able and display them I had to do some serious research as it is a pain. Hardly any information out there somehow. Here is what I did:

I also tried every possible //\, ////, combination of those and IP and what else... no way to get it work.

But what actually is working was/is an upload to \\IE-NAS-01\Data\Pictures for example. This way I can use it since the PHP is allowed to do so once you set up the rights for those kind of actions.

It is that HTML is not granted to step outside your wwwroot directory and so you can't access any files on your server or network. But once you've set up your virtual directory it is "part" of the wwwroot.

I hope this will help

Cheers

Edit: You will find some more here: - Stack Overflow #13421784 - https://stackoverflow.com/a/258380/8188398

Display a picture outside root with php:

<img src="http://path.to/this/file.php"/>

file.php:

<?php
header('Content-Type: image/jpeg');
readfile('path/to/image.jpeg');

This way you can avoid the restriction! Cheers!

PS: Content-Types:

header('Content-Type: image/gif');
header('Content-Type: image/jpeg');
header('Content-Type: image/png');
floGalen
  • 262
  • 2
  • 17
  • So as you can read up on those other two questions there is actually a php solution for that. But it wont work with simply html. Either use a virtual directory or php in order to display images on your network. Cheers :) – floGalen Sep 01 '17 at 15:20
  • Thanks for the suggestion, but I've to allow for there being 100s of mapped/shared locations which are not known ahead of time, so creating virtual directories for each is not possible. I'm sure in a lot of cases this would be a good solution – user48408 Sep 04 '17 at 13:08
  • well you might can run some kind of `.bat file` to create those VRs. But in order to display those images with pure HTML you can't use file:// as src. Also for example if you want to link to one of those pictures, every link with `file://` will not work in Chrome since this is made for security purposes! One way would be to look up the picture with php and display it this way. Then you avoid the restrictions which are given by the server/html – floGalen Sep 04 '17 at 14:32
  • Hi flo. I don't need it to work in Chrome. I only need it to work IE. Its an intranet application for a handful of users. Browser choice is not important.. I've seen numerous posts online from people wanting this to work in chrome/ff having HAD success in IE by using file:// . But when I've used the same approach they've shown i can't get them to load. For excample, see accepted answer https://www.experts-exchange.com/questions/21830397/How-to-display-image-that-has-UNC-path.html. I thought maybe it only used to work with older versions of IE but i've used an emulator and had the same results. – user48408 Sep 05 '17 at 15:42
  • In order to make a UNC path work I assume you would have to open up your server for your network and allow http to step outside your root. How to do this or if it even is possible I don't know. *.php solution is not working for you? – floGalen Sep 05 '17 at 16:48