In My webpage i am using a image tag, the src attribute is pointing to shared network location ie (/server/images/image1.png). The exact script is "<img src="file://///server/images/image1.png"
. It is working fine in IE. In firefox, when I do debug using firebug its showing image, but it's not displayed in page (user view). Even it's working fine when copy this location and place it in firefox address bar. What will be the problem while using img tag also what is the solution for this? Thanks in advance.
-
1Is the web page being served via HTTP or also via the file: protocol? – Aaron J Spetner Jan 04 '12 at 19:08
-
@AaronJSpetner: It doesn't matter if it is HTTP or file: protocol. It should display it regardless. – Aaron Brewer Jan 04 '12 at 19:18
-
@AaronJSpetner web page is being served via http – Palani Jan 04 '12 at 20:09
5 Answers
Putting this as an answer here so as to provide help for others like myself that was searching for how to display networked images and came accross this SO post in the top 3 search engine results. It also seems like a better answer than the java servlet issuing images in the response.
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

- 9,522
- 8
- 54
- 76
Create a regular HTML img element like so:
<img id="image1" runat="server" ImageUrl=<%# Eval("Value") %>/>
And in code behind do this:
image1.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes);
Where bytes is a byte[]
.
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["FileName"] != null)
{
// Read the file and convert it to Byte Array
string filePath = "C:\\Users\\Public\\Pictures\\Sample Pictures\\";
string filename = Request.QueryString["FileName"];
string contenttype = "image/" + Path.GetExtension(filename).Replace(".", "");
FileStream fs = new FileStream(filePath + filename, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
br.Close();
fs.Close();
image1.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes);
}
}
You are done. The image will be displayed.

- 141
- 4
The solution usually is: use a web server.

- 112,730
- 33
- 157
- 176
-
The shared system does not have any web server. i need to take the image from the shared location(folder). not possible? FYI: Its working fine in IE. – Palani Jan 04 '12 at 19:17
-
If everyone uses IE, sure. A shared file system is not a replacement for a web server. Your setup will only lead to more trouble as time goes on. – Diodeus - James MacFarlane Jan 04 '12 at 19:20
-
My client is not having any web server installed in their system.Instead they asking me to implement this using shared location. Any more idea? – Palani Jan 04 '12 at 19:29
-
2@Diodeus: You do not need a Web Server to display HTML... This answer is not thought out very well. – Aaron Brewer Jan 04 '12 at 19:33
-
This setup is only going to lead to an unmaintainable mess. – Diodeus - James MacFarlane Jan 04 '12 at 19:34
You may have to make it like so.
<img src="../server/images/image1.png" />
Once you add the "../" it is basically telling your browser to go back one directory to search for the location after the "../" .
Where is the file located and where is the location of your HTML document?
UPDATE:
I do all of my work on a Network Server as well... This should do the trick.
<img alt="" src="file:///SERVER:/FOLDER/IMAGES/image1.png" />
Thanks, Aaron

- 3,567
- 18
- 48
- 78
-
Here server means its another system connected in same network and the folder is shared in network. – Palani Jan 04 '12 at 19:23
-
@Palani: I'd also recommend, if you are going to do some work on a server, then you might as well do it ALL from the server. Or else you may have problems. You do not need a WEB SERVER to display HTML... Regarding Diodeus answer below. – Aaron Brewer Jan 04 '12 at 19:28
-
1It seems your updated answer is same as my question. It is working fine in IE but not in any other browsers. – Palani Jan 04 '12 at 19:32
-
@Palani: You have FIVE Slahes in-front of file: and you do not have a colon in front of the server.... – Aaron Brewer Jan 04 '12 at 19:39
-
@Palani: And like I said work off the server do not work of your local hard drive and then some other parts off of your shared network drive. – Aaron Brewer Jan 04 '12 at 19:40
-
@Palani: Try also cache-refreshing (Ctrl +F5) and or try to see if Internet Explorer is stuck in Quirks Mode. Go to developer tools, and change the standards and browser mode. – Aaron Brewer Jan 04 '12 at 19:41
-
@Palani: And if you are working off the local drive instead of making the src of the image the whole path try... "images/image1.png" as the src. – Aaron Brewer Jan 04 '12 at 19:43
-
It is not helping me.Updated answer giving error "failed to get the path" – Palani Jan 04 '12 at 19:52
-
@Palani: I tried helping but with the lack of code and filepath locations, etc. The question is hard to solve. – Aaron Brewer Jan 04 '12 at 20:14
I wrote a servlet which reads the file from LAN using Java File Stream and sets it in response and it does the trick. Thanks to all for valuable response.

- 1,891
- 7
- 31
- 42