Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法从“我的web应用”中的其他服务器获取图像_C#_Asp.net_Webforms - Fatal编程技术网

C# 无法从“我的web应用”中的其他服务器获取图像

C# 无法从“我的web应用”中的其他服务器获取图像,c#,asp.net,webforms,C#,Asp.net,Webforms,我正在尝试使用网络上的IP地址从另一台服务器检索图像,但无法在我的web应用程序中获取图像 我尝试了以下方法写入图像路径,但无法获取图像: C#: string imagePath = @"http://192.168.10.245/Shared/1.jpg"; <asp:Image runat="server" ID="emp_img" CssClass="imgstyle" /> <img src="ImgHandler.ashx?EmpCode=1" styl

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

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

C#:

string imagePath = @"http://192.168.10.245/Shared/1.jpg";
  <asp:Image runat="server" ID="emp_img" CssClass="imgstyle" />
  <img src="ImgHandler.ashx?EmpCode=1" style="max-width:250px; max-height:250px;" />
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;
        }
    }
<add key="ProfilePhotoPath" value="\\\\192.168.10.245\\Pic\\"/>

aspx:

string imagePath = @"http://192.168.10.245/Shared/1.jpg";
  <asp:Image runat="server" ID="emp_img" CssClass="imgstyle" />
  <img src="ImgHandler.ashx?EmpCode=1" style="max-width:250px; max-height:250px;" />
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;
        }
    }
<add key="ProfilePhotoPath" value="\\\\192.168.10.245\\Pic\\"/>
aspx:

string imagePath = @"http://192.168.10.245/Shared/1.jpg";
  <asp:Image runat="server" ID="emp_img" CssClass="imgstyle" />
  <img src="ImgHandler.ashx?EmpCode=1" style="max-width:250px; max-height:250px;" />
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;
        }
    }
<add key="ProfilePhotoPath" value="\\\\192.168.10.245\\Pic\\"/>


我仍然无法获取图像,请帮助我写入路径。Web浏览器拒绝加载UNC路径或本地文件路径引用的资源-这是出于安全目的,以便公共Internet网页无法窥探您的网络或计算机中的文件

要显示这样的图像,ASP.NET应用程序需要先代理加载图像本身


您可以在WebForms中通过创建一个
*.ashx
处理程序来实现这一点,该处理程序将文件路径作为查询字符串接受,然后尝试加载文件本身,但请确保对输入进行清理,因为攻击者和恶意(或傻瓜)用户可以使用该处理程序获取web服务器有权访问的任何文件(请参阅:)以下代码解决了我的问题:

ashx:

string imagePath = @"http://192.168.10.245/Shared/1.jpg";
  <asp:Image runat="server" ID="emp_img" CssClass="imgstyle" />
  <img src="ImgHandler.ashx?EmpCode=1" style="max-width:250px; max-height:250px;" />
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;
        }
    }
<add key="ProfilePhotoPath" value="\\\\192.168.10.245\\Pic\\"/>
.CS:

web.config:

string imagePath = @"http://192.168.10.245/Shared/1.jpg";
  <asp:Image runat="server" ID="emp_img" CssClass="imgstyle" />
  <img src="ImgHandler.ashx?EmpCode=1" style="max-width:250px; max-height:250px;" />
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;
        }
    }
<add key="ProfilePhotoPath" value="\\\\192.168.10.245\\Pic\\"/>


\\192.168.10.245\Shared\1.jpg
如果您想使用windows网络而不是HTTP@RobinBennett它不起作用了。在这种情况下如何使用HTTP?要使用HTTP,您不能只使用路径(这需要windows网络获取路径,然后假装它是本地文件)。您需要执行HTTP请求并检索数据。此处的详细信息:服务器究竟是如何共享图像的?我如何才能做到这一点?我已经创建了一个图像处理程序,您能否帮助我在图像处理程序中写入获取图像的路径?@AlinaAnjum当浏览器通过您的处理程序请求图像时会发生什么情况?