Search code examples
c#asp.net-4.0server.mappath

Accessing image from other server


I have image file placed on the one server and application on other server.

I want to access that image, below code I have written:

On default.aspx, I have

 <asp:Image ID="Image1" runat="server"  ImageUrl= "GetImage.aspx?imgName=MyImage.jpg" />

and on GetImage.aspx, I have written the below code on page_load

 protected void Page_Load(object sender, EventArgs e)
    {
        // Changing the page's content type to indicate the page is returning an image
        Response.ContentType = "image/jpg";
        var imageName = Request.QueryString["imgName"];
        var path = "//SERVER/FOLDER/" + imageName;


        if ((string.IsNullOrEmpty(imageName) == false))
        {
            // Retrieving the image
            System.Drawing.Image fullSizeImg;
            fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(path));
            // Writing the image directly to the output stream
            fullSizeImg.Save(Response.OutputStream, ImageFormat.Jpeg);
            // Cleaning up the image
            fullSizeImg.Dispose();
        }
    }

But I am getting error at

fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(path));

Please let me know where I am incorrect. Do I need to anything else other than Server.MapPath ? because my image is on other server.

EDIT

  • I have image folder in my computer
  • I have created a web app in other computer [same network], deployed in IIS, image is displayed correctly. With path like http://10.67.XX.XX/websiteName/Default.aspx
  • but when I am trying to access the same from my comupter or any other computer, I am not able to see the image.

Solution

  • You shouldn't use Server.MapPath. This is used to map virtual paths under your site to physical paths under file system. If the file exists on another server, just access it by name directly, without Server.MapPath.