首页 > 解决方案 > 无法从我的网络应用程序中的其他服务器获取图像

问题描述

我正在尝试使用通过网络的 IP 地址从其他服务器检索图像,但无法在我的 Web 应用程序中获取图像。

我尝试了以下方法来编写图像路径但无法获取图像:

C#:

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

或者

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

或者

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

或者

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

emp_img.ImageUrl = imagePath;

aspx:

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

请注意,图像放置在共享文件夹中,该文件夹在 Windows 资源管理器中可以正常打开

请帮我解决这个问题

我经历过这个

谢谢

更新:

我创建了一个图像处理程序来实现这一点:

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;" />

我仍然无法获取图像,请帮我写路径

标签: c#asp.netwebforms

解决方案


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\\"/>

推荐阅读