开发者

Show Images from outside of ASP.NET Application

开发者 https://www.devze.com 2023-02-05 02:13 出处:网络
So I\'m having some trouble displayi开发者_如何转开发ng images from outside of the project folder...

So I'm having some trouble displayi开发者_如何转开发ng images from outside of the project folder...

I seem to be only able to access images within the "~" directory and subdirectories...

Say, if I want to access images from "E:/XYZ/11-01-01 New Year/" or something like that how may I do so?

Note: I set privileges on all folders and sub folders as Readable to "Everyone" so IIS/Visual Studio should be able to but isn't showing the images in the Image Control


You should use an HttpHandler to show the images.

Googling for "httphandler images" turns up numerous examples on how to do this.

Instead of trying to link directly to you images, you would link to the handler with the querystring determining which image to show. eg

http://mysite.com/MyHandler.ashx?image=myimage.jpg

Here's a very basic sample (minus error handling etc)

using System.IO;
using System.Web;
public class MyImageHandler : IHttpHandler 
{
    public void ProcessRequest(System.Web.HttpContext ctx) 
    {

        string _Path;

        _Path = "E:\\XYZ\\11-01-01 New Year\\" + context.Request.QueryString["image"];

        ctx.Response.StatusCode = 200;
        ctx.Response.ContentType = "image/jpeg";
        ctx.Response.WriteFile(_Path);      
    }

    public bool IsReusable { get {return true; } }        
 }


The client browser will never know where those images are unless you map a virtual directory to the images. On your local test environment, you could work around this by referencing the image as "file:///E:/XYZ/11-01-01 New Year/myimage.jpg", but this is EXTREMELY bad practice and will never work in a production environment. You should always use relative paths within the application's working directory, or a statically defined virtual directory located elsewhere.

0

精彩评论

暂无评论...
验证码 换一张
取 消