Show Images from outside of ASP.NET Application

后端 未结 2 1498
陌清茗
陌清茗 2020-12-10 21:58

So I\'m having some trouble displaying images from outside of the project folder...

I seem to be only able to access images within the \"~\" directory and subdirecto

2条回答
  •  不思量自难忘°
    2020-12-10 22:26

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

提交回复
热议问题