0

I am trying to retrieve an image from the other server using IP Address which is over the network but Unable to get the image in my web app.

I've tried the following ways to write the image path but unable to get the image:

C#:

string imagePath = @"http://192.168.10.245/Shared/1.jpg";

OR

string imagePath = @"file://192.168.10.245/Shared/1.jpg";

OR

string imagePath = @"\\192.168.10.245\Shared\1.jpg";

OR

string imagePath = @"192.168.10.245/Shared/1.jpg";

emp_img.ImageUrl = imagePath;

aspx:

  <asp:Image runat="server" ID="emp_img" CssClass="imgstyle" />

kindly note that the image is placed in a Shared Folder which is opening fine in Windows Explorer

kindly help me resolve this

I've gone through this

Thanks

UPDATE:

I've created an Image Handler to achieve this:

ImgHandler.ashx:

public class ImgHandler : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "image/jpg";
        string EmpCode = context.Request.Params["EmpCode"].ToString();
        string path = "//192.168.10.245\\Shared\\"+EmpCode+".jpg";
        context.Response.WriteFile(path);
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

aspx:

  <img src="ImgHandler.ashx?EmpCode=1" style="max-width:250px; max-height:250px;" />

I'm still unable to get the image, kindly help me writting the path

Alina Anjum
  • 1,178
  • 6
  • 30
  • 53
  • `\\192.168.10.245\Shared\1.jpg` should work, if you want to use windows networking rather than HTTP – Robin Bennett Nov 06 '19 at 10:46
  • @RobinBennett it's not working. How can I use HTTP in this scenario? – Alina Anjum Nov 06 '19 at 10:49
  • To use HTTP, you can't just use a path (which requires windows networking to get it and then pretend that it's a local file). You need to perform an HTTP request and retrieve the data. Details here: https://stackoverflow.com/questions/3615800/download-image-from-the-site-in-net-c – Robin Bennett Nov 06 '19 at 10:51
  • how exactly is the server sharing the image? – BugFinder Nov 06 '19 at 11:06

2 Answers2

0

Web browsers refuse to load resources referenced by UNC paths or local file paths - this is for security purposes so that public Internet web-pages cannot snoop your network or computer for files.

To display images like this your ASP.NET application will need to proxy-load the image itself first.

You can do this in WebForms by creating an *.ashx handler that accepts the file path as a querystring and then attempts to load the file itself - but be sure to sanitize the input because this can be used by attackers and malicious (or idiot) users to get any file the web-server has access to (see: https://en.wikipedia.org/wiki/Directory_traversal_attack )

Dai
  • 141,631
  • 28
  • 261
  • 374
0

The following code resolved my Problem:

ashx:

public void ProcessRequest (HttpContext context) {
        try
        {
            context.Response.ContentType = "image/jpg";
            string FileName = context.Request.Params["FileName"].ToString();
            string path = System.Configuration.ConfigurationManager.AppSettings["ProfilePhotoPath"].ToString()+FileName;
            byte[] pic = GetImage(path);
            if (pic != null)
            {
                context.Response.WriteFile(path);
            }
        }
        catch (Exception ex)
        {

        }


    }

    private byte[] GetImage(string iconPath)
    {
        using (WebClient client = new WebClient())
        {
            byte[] pic = client.DownloadData(iconPath);
            //string checkPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +@"\1.png";
            //File.WriteAllBytes(checkPath, pic);
            return pic;
        }
    }

.CS:

emp_img.ImageUrl = "ImgHandler.ashx?FileName=1.jpg";

web.config:

<add key="ProfilePhotoPath" value="\\\\192.168.10.245\\Pic\\"/>
Alina Anjum
  • 1,178
  • 6
  • 30
  • 53