2012-03-21 44 views
0

我正尝试在C#中编写HTTP处理程序,该程序从网络驱动器加载映像并将它们写入HTTP响应。目前这不适用于我,因为我不断收到HTTP 302响应,导致显示的文件图像中断。以下是我的HTTP处理程序。访问权限已设置,因此匿名用户可以读取共享,但理想情况下这不会是永久性的。.ashx HTTP处理程序无法将映像从网络共享写入HTTP响应

public class SecCamImage : IHttpHandler, IRequiresSessionState 
{ 
    public void ProcessRequest(HttpContext context) 
    { 
     Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.Config");   
     KeyValueConfigurationElement setting = null; 

     if(config.AppSettings.Settings.Count > 0) 
     { 
      setting = config.AppSettings.Settings["CameraBaseURL"]; 
     } 

     if(setting != null) 
     { 
      string baseURL = setting.Value; 
      string location = context.Request["location"].ToString(); 
      string camera = context.Request["camera"].ToString(); 
      string image = context.Request["image"].ToString(); 

      if (!(string.Compare(image, "no-image.jpg", true) == 0)) 
      { 
       if (!string.IsNullOrEmpty(location) && !string.IsNullOrEmpty(camera) && !string.IsNullOrEmpty(image)) 
       { 
        string fullPath = string.Format(baseURL, location, camera, image); 

        System.IO.FileInfo imageFile = new System.IO.FileInfo(fullPath); 

        if (imageFile.Exists) 
        { 
         if (context.User.Identity.IsAuthenticated) 
         { 
          context.Response.ContentType = "image/jpeg"; 
          context.Response.WriteFile(imageFile.FullName); 
          context.Response.End(); 
         } 
        } 
       } 
      } 
      else 
      { 
       context.Response.ContentType = "image/jpeg"; 
       context.Response.WriteFile(image); 
       context.Response.End(); 
      } 
     } 
    }  

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

存储在配置文件中的URL的结构是这样的: -

\\\\host\\directory\\{0}\\{1}\\{2} 

{0}{1}是目录和{2}是文件。

回答

2

我设法通过在我们的IIS网站上添加一个虚拟目录来实现这个目标。 .ashx处理程序现在引用Virutal目录而不是网络驱动器上的目录。