Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display image stored on the network in asp.net (c#)

Tags:

c#

asp.net

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>
like image 480
A.J Avatar asked Oct 28 '25 14:10

A.J


2 Answers

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);
}
like image 128
Paul Zahra Avatar answered Oct 31 '25 04:10

Paul Zahra


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.
like image 23
Alexei Levenkov Avatar answered Oct 31 '25 05:10

Alexei Levenkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!