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}
是文件。