简体   繁体   中英

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

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

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 Question and i updated my code as below

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.

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:

    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:

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

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

The other page can . However, the other page isn't accessing 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:

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

Since the user doesn't have access to that file, the img tag 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.

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

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM