无法从Web应用程序中的“其他”服务器获取图像

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

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

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

请注意,图像位于Windows资源管理器中可以正常打开的共享文件夹中

请帮助我解决这个问题

我经历了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;" />

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

niuliang1023 回答:无法从Web应用程序中的“其他”服务器获取图像

Web浏览器拒绝加载UNC路径或本地文件路径引用的资源-这是出于安全目的,以便公共Internet网页无法窥探您的网络或计算机上的文件。

要显示此类图像,您的ASP.NET应用程序将需要首先代理加载图像本身。

您可以在WebForms中通过创建一个*.ashx处理程序来执行此操作,该处理程序将文件路径作为查询字符串接受,然后尝试加载文件本身-但请确保清除输入内容,因为攻击者和恶意(或白痴)用户获取Web服务器有权访问的任何文件(请参阅:https://en.wikipedia.org/wiki/Directory_traversal_attack

,

以下代码解决了我的问题:

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\\"/>
本文链接:https://www.f2er.com/3152982.html

大家都在问