【问题标题】:How to display image which is stored on Local drive or network drive?如何显示存储在本地驱动器或网络驱动器上的图像?
【发布时间】:2013-10-09 13:01:58
【问题描述】:

我有一个 asp 页面,我必须在其中显示存储在本地磁盘 C 中的图像:在某些情况下来自网络驱动器 示例路径:--C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg 我在后面的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);

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

对于显示图像的页面

        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);

对于代理页面

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

这很好,但我有两个问题

 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 ?

任何帮助将非常感谢。

【问题讨论】:

  • 您可能希望限制ImageWriter.aspx 可以处理的文件范围:您现在可以访问网络服务器上“IIS 用户”可以读取的任何文件。
  • 是的,但我可以像@David 所说的那样强制用户进行身份验证

标签: c# asp.net image web-applications


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    图像不可用,因为该路径与可能在另一台 PC 和网络上的浏览器的 asp.net 站点用户没有相对上下文。要解决此问题,您需要做的就是创建一个 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);
        }
    

    然后在您的 asp 中将图像 url 指向 ashx:

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

    【讨论】:

    • 我将您的 ProcessRequest 添加到一个名为 images.ashx 的文件中,这对我来说非常有效。我还添加了一个 try catch。谢谢!
    【解决方案3】:

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

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

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

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

    2) 还有其他方法吗?

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

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

    【讨论】:

    • 谢谢,我想我明白了。
    猜你喜欢
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    相关资源
    最近更新 更多