从不同的LAN远程服务器载入图片 [英] Loading image from remote server in different LAN

查看:263
本文介绍了从不同的LAN远程服务器载入图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的堆栈ASP.NET(南希)用的Razor视图引擎。但是,这可能涉及我想所有技术。

我有两个局域网(我们姑且称之为大网和小网)和两个服务器(a.k.a主服务器和镜像服务器)。主服务器可以通过电脑从两个网络达成,但图像服务器只适用于小型网络设备是可见的。我已经通过网站,显示图片来自远程(图)服务器主服务器提供服务:

My stack is ASP.NET (Nancy) with Razor view engine. But this may concern all technologies I suppose.
I have two LAN networks (let's call them Large network and Small network) and two servers (a.k.a Main server and Image server). Main server can be reached by computers from both networks but Image server is visible only for machines in Small network. I have website served by Main server which shows images from remote (Image) server:

<img src="http://ImageServer/image.png"/>

在从小型网络计算机加载从主服务器的网页,因为它可以访问图像服务器太(同一网络)一切都很好。当从大型网络的计算机加载网页会出现问题。图像不加载,因为图像服务器是小型网络这是不是从大型局域网内的机器访问。

有没有什么办法,迫使主服务器直接服务形象?

All is fine when computer from Small network is loading the webpage from Main server as it has access to Image server too (the same network). Problem occurs when the computer from Large network loads the webpage. The image is not loading because Image server is in Small network which is not accessible for machines from Large LAN.
Is there any way to force Main server to serve the image directly?

推荐答案

我不知道为什么我找不到<一个href=\"http://stackoverflow.com/questions/8496195/how-to-view-images-of-local-network-from-remote-interface-when-using-img-src\">this前张贴我的问题。最简单的方法似乎是正确的路由器配置。但是...更改路由器配置是不可能在我的处境。知道的一切,我终于开始了我自己的大脑)。
我的解决方案在图像先用下面的C#代码片段转换为Base64字符串:

I don't know why I couldn't find this before posting my question. The simplest approach seems to be proper router configuration. But... changing router configuration is not possible in my situation. Knowing all that I started up my own brain finally ;). My solution is converting the image to base64 string first with the following C# snippet:

    private string convertToBase64(string imageURL)
    {
        var request = WebRequest.Create(imageURL);

        using (var response = request.GetResponse())
        {
            using (var stream = response.GetResponseStream())
            {
                using (Image image = Image.FromStream(stream))
                {
                    using (MemoryStream m = new MemoryStream())
                    {
                        image.Save(m, image.RawFormat);
                        byte[] imageBytes = m.ToArray();
                        string base64String = Convert.ToBase64String(imageBytes);
                        return string.Format("data:image/png;base64,{0}", base64String);
                    }
                }
            }
        }
    }

这一切都发生在服务器端所以没有能够访问图像的问题。

然后,我通过Model属性传递字符串来查看和显示图像如下:

It's all happening at the server side so no issues with access to image.
Then I'm passing the string to the View via Model property and displaying the image as follows:

<img id="photo" src="@Model.Base64Image" title="@Model.Description" />

也许这解决方案有它的缺陷,但它为我工作,我无法找到任何东西,我可以在这种情况下使用。

Probably this solution has it's flaws, but it works for me, and I couldn't find anything else that I could use in this case.

这篇关于从不同的LAN远程服务器载入图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆