Accessing image from other server

倖福魔咒の 提交于 2019-12-10 11:20:06

问题


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.

回答1:


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.




回答2:


The answer above is incorrect because the images reside on a separate server.

So System.Images will not no where the image is.

Secondly you have to use server.Mappath for system images, it requires a windows path i.e. c:\blah\blah\whatever.jpg. you will need to use \\server\C$\folder\image.jpg this works well with system.image.FromFile

Also saving you can also utilize this.

Cheers



来源:https://stackoverflow.com/questions/7970755/accessing-image-from-other-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!