4

How to display image stored on the network in asp.net (using c#). I want img src to be the one given below:

\\pgapp\folder\image.jpg, where pgapp is the shared drive on the network.

I am using these codes but its not displaying the image on the webpage.

originalImage2.ImageUrl=@"\\pgapp\folder\image.jpg";

or

originalImage2.Attributes["src"]=ResolveUrl(@"\\pgapp\folder\image.jpg");

<div ID="imgContainer" runat="server" style="width: 400px; height: 400px;  overflow:auto; border: solid 1px black;
                    padding: 10px; margin-bottom: 5px;" >

                    &nbsp;<asp:Image ID="originalImage2" runat="server" />
                </div>
1
  • Where is the image src pointing to when you do a debug? Commented Apr 4, 2012 at 2:00

3 Answers 3

4

To display images stored on a network in a users web browser do something like the following:

Convert the image to it's base64 counterpart and display:

// PhotoId is the network file path and name.
string photoId = "\\Photos\2015-May\390828d1-8f20-4fe9-9287-13d03894e9c0.jpg"

// Call to display the networked images.
lbl_images.Text += "<img src='" + this.PhotoBase64ImgSrc(photoId) + "' height='60px' width='60px' alt='photo' />";

// Supporting function that converts an image to base64.
protected string PhotoBase64ImgSrc(string fileNameandPath)
{
    byte[] byteArray = File.ReadAllBytes(fileNameandPath);
    string base64 = Convert.ToBase64String(byteArray);

    return string.Format("data:image/gif;base64,{0}", base64);
}
Sign up to request clarification or add additional context in comments.

6 Comments

How would you display the image in WebForms?
This doesn't work because modern browsers don't allow access to files outside the cache of the website for security reasons. So if it's C:\outsidecache\file.jpg it wouldn't work. I tried both \\sharedfolder\file.jpg and file://sharedfolder/file.jpg and it just won't work.
@sojim2 I deleted my comment because it was wrong... I was talking about windowsforms not webforms... I use this in a webforms application... you must run the app pool in IIS under an account which has access to the networked location... the app pool can then read the image file, convert it to base64 and send it to the browser as a string.... then use it like <img src="PUTTHEBASE64STRINGHERE" />
I found an easier method, virtual directories!
Then you don't mind your images being available to the public? Why not just put them in a folder of the website itself? (If only one website is to access the images).
|
2

You will hit 2 problems:

  • Browsers normally can't access server shares (unless it is on the same network). You need to expose image through your site somehow so it visible like http://myserver/image/image1.jpg
  • "NTLM one hop" - your server will not be able to read files from remote server if you start rendering image through your server. You just need to verify if you have the problem and read about it.

Comments

1

If you want to display images which are store in the network path then change below setting in IIS,

  1. Navigate to IIS.
  2. Navigate to website in Sites folder.
  3. Select application website and navigate to right side Actions menu and click on View Virtual Directories. enter image description here
  4. Click on Add Virtual Directories and specify an alias as "images" and physical path as "network drive path". enter image description here enter image description here
  5. Test settings and Save it.
  6. Refresh application pool and test whether image is rendering or not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.