简体   繁体   English

无法从我的 web 应用程序中的其他服务器获取图像

[英]Unable to get the Image from the Other server in my web App

I am trying to retrieve an image from the other server using IP Address which is over the network but Unable to get the image in my web app.我正在尝试使用 IP 地址从其他服务器检索图像,该地址通过网络但无法在我的 web 应用程序中获取图像。

I've tried the following ways to write the image path but unable to get the image:我尝试了以下方法来编写图像路径但无法获取图像:

C#: 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: aspx:

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

kindly note that the image is placed in a Shared Folder which is opening fine in Windows Explorer请注意,图像放置在共享文件夹中,该文件夹在 Windows Explorer 中可以正常打开

kindly help me resolve this请帮我解决这个问题

I've gone through this我经历过这个

Thanks谢谢

UPDATE:更新:

I've created an Image Handler to achieve this:我创建了一个图像处理程序来实现这一点:

ImgHandler.ashx: 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: aspx:

  <img src="ImgHandler.ashx?EmpCode=1" style="max-width:250px; max-height:250px;" />

I'm still unable to get the image, kindly help me writting the path我仍然无法获取图像,请帮我写路径

Web browsers refuse to load resources referenced by UNC paths or local file paths - this is for security purposes so that public Internet web-pages cannot snoop your network or computer for files. Web 浏览器拒绝加载 UNC 路径或本地文件路径引用的资源 - 这是出于安全目的,因此公共 Internet 网页无法窥探您的网络或计算机的文件。

To display images like this your ASP.NET application will need to proxy-load the image itself first.要显示这样的图像,您的 ASP.NET 应用程序需要先代理加载图像本身。

You can do this in WebForms by creating an *.ashx handler that accepts the file path as a querystring and then attempts to load the file itself - but be sure to sanitize the input because this can be used by attackers and malicious (or idiot) users to get any file the web-server has access to (see: https://en.wikipedia.org/wiki/Directory_traversal_attack )您可以在 WebForms 中通过创建一个*.ashx处理程序来执行此操作,该处理程序接受文件路径作为查询字符串,然后尝试加载文件本身 - 但请务必清理输入,因为攻击者和恶意(或白痴)可以使用它用户获取网络服务器有权访问的任何文件(参见: https://en.wikipedia.org/wiki/Directory_traversal_attack

Put Your Image Inside a Placeholder....Hope it is useful to you.......将您的图像放在占位符中....希望对您有用....

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="SC1" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="UP1" runat="server">
        <ContentTemplate>
           <asp:PlaceHolder ID="PlaceHolder1" runat="server">
            <asp:Image ID="Image1" runat="server" />
           </asp:PlaceHolder>
        </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html>

In Code Behind......在代码背后......

  protected void Page_Load(object sender, EventArgs e)
    {
        string str = "~/Images/Accept.png/"; ----Specify Your Url In Proper Way...
        Image1.ImageUrl = str;

    }

Note:-笔记:-

PlaceHolder is an asp.net web server control which used to store dynamically added web server controls on the web page. PlaceHolder 是一个 asp.net web 服务器控件,用于在 Z25267A5EC9705EB7 页面上存储动态添加的 web 服务器控件。

For More Info about...PlaceHolder Refer this Below Link...有关... PlaceHolder 的更多信息,请参阅下面的链接...

https://asp-net-example.blogspot.com/2009/02/aspnet-placeholder-example-how-to-use.html https://asp-net-example.blogspot.com/2009/02/aspnet-placeholder-example-how-to-use.html

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: 。CS:

emp_img.ImageUrl = "ImgHandler.ashx?FileName=1.jpg";

web.config: web.config:

<add key="ProfilePhotoPath" value="\\\\192.168.10.245\\Pic\\"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我会收到无法从 Web 应用程序而不是控制台应用程序连接到远程服务器的异常? - Why do I get an Unable to connect to the remote server exception from a web app and not console app? 无法从服务器获取完整图像 - Unable to get full image from server 无法从另一台PC连接到我的C#服务器 - Unable to connect to my C# server from an other pc 如何让 Web 应用程序从不同的服务器提供静态文件 - How to get a web app to serve static files from a different server 无法在IIS服务器和客户端上启动Web应用程序 - Unable to launch web app on IIS Server and client 从其他服务器访问图像 - Accessing image from other server 从我的局域网上的其他计算机访问C#Web应用程序时出错 - Error accessing C# web app from other machine on my LAN 从MVC控制器执行发布时无法在Web Api中获取整个图像? - Unable to get the entire Image in web Api while doing Post from the MVC controller? 使用WMI或其他安全方法从远程服务器获取IIS设置(位于web.config中) - Get IIS settings (located in web.config) from remote server using WMI or other secure methods 在ASP.net中的其他网站上共享服务器中的图像,例如照片托管服务 - Share an image from my server on other websites in ASP.net, like photo hosting services
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM