Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
125 views
in Technique[技术] by (71.8m points)

c# - Show Images from outside of ASP.NET Application

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...