C# 如何显示存储在本地驱动器或网络驱动器上的图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19272842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:33:36  来源:igfitidea点击:

How to display image which is stored on Local drive or network drive?

c#asp.netimageweb-applications

提问by

I have a asp page in which i have to display the image which is stored in my local disk C: and in some cases from network drive Example path:--C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg I have done the following code in c# code behind

我有一个 asp 页面,我必须在其中显示存储在本地磁盘 C: 中的图像,在某些情况下来自网络驱动器示例路径:--C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg I在c#代码后面做了下面的代码

Image image = new Image();
image.Height = 150;
image.Width = 150;
string path = @"C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg";//this path will come from database dynamically this is for test only
image.ImageUrl = path;
this.Controls.Add(image);

but image is not showing up so i stumbled upon the this SO Questionand i updated my code as below

但图像没有显示,所以我偶然发现了这个 SO 问题,我更新了我的代码如下

for page which is showing image

对于显示图像的页面

        Image image = new Image();
        image.Height = 150;
        image.Width = 150;
        string path = @"C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg";
        image.ImageUrl = "ImageWriter.aspx?path=" + path;
        this.Controls.Add(image);

and for proxy page

和代理页面

        Response.ContentType = "image/jpeg"; // for JPEG file
        string physicalFileName = Request.QueryString["path"];
        Response.WriteFile(physicalFileName);

this is working fine but i have two question

这工作正常,但我有两个问题

 1) Why the file is accessible from the physical path while proxy from page but other page cannot able to access it ? 
 2) And is there any other way to do it ?

Any help would be great thanks.

任何帮助都会非常感谢。

采纳答案by David

1) Why the proxy page can access the physical path but other page cannot able to access it ?

1)为什么代理页面可以访问物理路径而其他页面无法访问?

The other page can. However, the other page isn'taccessing the file. It's just setting the file's path to the HTML output for the client(web browser) to access it. Resulting in this:

其他页面可以。但是,另一个页面没有访问该文件。它只是将文件的路径设置为客户端(Web 浏览器)的 HTML 输出以访问它。结果是这样:

<img src = "C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg" />

Since the user doesn't have access to that file, the imgtag won't work. The purpose of the "proxy" page is to serve the content of the file from a location which the user can access, that location being the proxy page.

由于用户无权访问该文件,因此该img标签将不起作用。“代理”页面的目的是从用户可以访问的位置提供文件内容,该位置是代理页面。

2) And is there any other way to do it ?

2)还有其他方法可以做到吗?

Nope. Either the user has to have direct access to the file via some virtual path on the web server, or you have to have a proxy which reads the file's contents and sends it to the user. It has little to do with your C# code and everything to do with what the user can see on the web server. Unless the file is in a web-accessible folder, the user can't see it.

不。要么用户必须通过 Web 服务器上的某个虚拟路径直接访问该文件,要么您必须有一个代理来读取文件的内容并将其发送给用户。它与您的 C# 代码无关,而与用户可以在 Web 服务器上看到的内容有关。除非文件位于可通过网络访问的文件夹中,否则用户无法看到它。

This is a very common and standard approach to securing files behind a web application. Sometimes, for example, you want to check the user's authorization before allowing them to see a file. If the file were publicly accessible, they could just go to it directly. But if there's a proxy page between the user and the file then that page can enforce authorization.

这是在 Web 应用程序背后保护文件的一种非常常见和标准的方法。有时,例如,您希望在允许用户查看文件之前检查用户的授权。如果该文件可公开访问,则他们可以直接访问该文件。但是如果用户和文件之间有一个代理页面,那么该页面可以强制执行授权。

回答by guymid

The images are not available because that path has no relative context to an asp.net site user through a browser who will probably be on another PC and network. To fix this, all you need to do is create an ASHX handler page that fetches the images that are on the server's local drive or network and serve them as an image to the browser:

图像不可用,因为该路径与通过浏览器的 asp.net 站点用户没有相关上下文,而浏览器可能位于另一台 PC 和网络上。要解决此问题,您需要做的就是创建一个 ASHX 处理程序页面,该页面获取服务器本地驱动器或网络上的图像,并将它们作为图像提供给浏览器:

    public void ProcessRequest(HttpContext context)
    {
        string imgName = context.Request.QueryString["n"];
        context.Response.ContentType = "image/png";
        string path = @"C:\Users\Public\Pictures\Sample Pictures\" + imgName;
        Image image = Image.FromFile(path);
        image.Save(context.Response.OutputStream, ImageFormat.Png);
    }

And then in your asp just point an image url to the ashx:

然后在你的 asp 中将一个图像 url 指向 ashx:

    image.src = "images.ashx?n=penguin.jpg";

回答by Paolo

On my experience, the best way, just to avoid all the security problem that finally make impossible the above solutions, is to setup a local website and set the image url like this: image1.ImageUrl="http://pcname/photofolder/myphoto1.png". pcname is the name of the local web server and the path is the path of a virtual directory set in the local IIS. To setup a local IIS is very simple, google have tons of tips, and to setup a virtual directory is only needed to enter the iis, open the default website, right-clik on "add a new virtual directory" and set the name (photofolder in my example) and make it point to a physical local disk folder and the trick is done. This has also a vantage, that's to say that you can access that folder in all clients connected to the lan.

根据我的经验,为了避免最终使上述解决方案变得不可能的所有安全问题,最好的方法是设置一个本地网站并像这样设置图像网址:image1.ImageUrl="http://pcname/photofolder/ myphoto1.png”。pcname 是本地 Web 服务器的名称,路径是本地 IIS 中设置的虚拟目录的路径。设置本地IIS很简单,google有很多提示,设置虚拟目录只需要进入iis,打开默认网站,右键“添加新的虚拟目录”并设置名称( photofolder 在我的示例中)并使其指向物理本地磁盘文件夹,技巧就完成了。这也有一个优势,也就是说您可以在连接到 LAN 的所有客户端中访问该文件夹。