<%@ WebHandler Language="c#" Class="Dcx.Web.DownloadFileHandler" Debug="true" %>
using System;
using System.Web;
using System.IO;
//using System.Drawing;
//using System.Drawing.Imaging;
//using System.Configuration;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Threading;
using Dcx.BLL;
using System.Drawing;

/// <summary>
///DownloadHandler 的摘要说明
/// </summary>
namespace Dcx.Web
{
    public class DownloadFileHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState 
    {
        static System.Collections.Hashtable ContentType = new Hashtable();

        static DownloadFileHandler()
        {
            //ContentType[".JS"] = "application/x-javascript";
            ContentType[".XML"] = "text/xml";
            ContentType[".HTML"] = "text/html";
            ContentType[".HTM"] = "text/html";
            ////ContentType[".CSS"] = "text/css";
            //ContentType[".TXT"] = "text";
            //ContentType[".PNG"] = "image";
            //ContentType[".BMP"] = "image";
            //ContentType[".GIF"] = "image";
            //ContentType[".JPG"] = "image";
            //ContentType[".ICO"] = "image";
            //ContentType[".CUR"] = "image";
        }

        public DownloadFileHandler()
        {
        }

        [System.Web.Services.WebMethod(EnableSession = true)]
        void IHttpHandler.ProcessRequest(HttpContext context)
        {
        
            int fileId = 0;
            string fileName = null;
            string filePath = null;
            if (context.Request["fileName"] != null && context.Request["fileName"].ToString().Trim() != "")
                fileName =Dcx.Common.Utils.UrlDecode( context.Request["fileName"].ToString());
            if (context.Request["filePath"] != null && context.Request["filePath"].ToString().Trim() != "")
            {
                filePath = Dcx.Common.Utils.UrlDecode(context.Request["filePath"].ToString());

                if (Dcx.Common.Utils.StrIsNullOrEmpty(fileName))
                    fileName =Path.GetFileName(filePath);
            }
            else
            {
                filePath = fileName;
                fileName = Path.GetFileName(filePath);
            }
            if (context.Request["fileId"] != null)
                fileId = Dcx.Common.TypeConverter.StrToInt(context.Request["fileId"].ToString());
            if (!Dcx.BLL.VonleHelper.IsLogined(context))
            {
                context.Response.Write("您还没登陆系统，不允许下载文件，下载失败。");
                return;
            }
            if (Dcx.Common.Utils.StrIsNullOrEmpty(filePath))
            {
                context.Response.Write("文件路径有误，下载失败。");
                return;
            }
            filePath = context.Server.MapPath(filePath);
            if (Dcx.Common.FileOperate.CheckFileIsInType(filePath))
            {
                context.Response.Write("这个类别的文件不允许下载，下载失败。");
                return;
            }
            if (Dcx.Common.FileOperate.CheckFileIsInType(fileName))
            {
                context.Response.Write("这个类别的文件不允许下载，下载失败。");
                return;
            }
            //ImServer.Instance.CheckPermission(context, filePath, IOPermission.Read);
            using (System.IO.Stream stream = File.Open(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
            {
                try
                {
                    if (Dcx.Common.Utils.StrIsNullOrEmpty(fileName))
                        fileName = Dcx.Common.Utils.UrlDecode(Microsoft.JScript.GlobalObject.escape(Path.GetFileName(filePath)));
                    fileName = Dcx.Common.Utils.UrlEncode(fileName);//  Microsoft.JScript.GlobalObject.escape(fileName));
                    string ext = Path.GetExtension(filePath).ToUpper();
                    if (ContentType.ContainsKey(ext))
                    {
                        context.Response.ContentType = ContentType[ext] as string;
                        context.Response.AppendHeader("Content-Disposition", string.Format("fileName={0}", fileName));//Dcx.Common.Utils.UrlDecode(Microsoft.JScript.GlobalObject.escape(fileName))));
                    }
                    else
                    {
                        context.Response.ContentType = "application/octet-stream";
                        context.Response.AppendHeader("Content-Disposition", string.Format("attachment;fileName={0}", fileName));//, Dcx.Common.Utils.UrlDecode(Microsoft.JScript.GlobalObject.escape(fileName))));
                    }


                    FileSystemInfo fileInfo = new FileInfo(filePath);
                    if (fileInfo != null)
                    {
                        context.Response.AppendHeader("Last-Modified", String.Format("{0:R}", fileInfo.LastWriteTime));
                    }

                    context.Response.AppendHeader("Content-Length", stream.Length.ToString());

                    byte[] buffer = new byte[4 * 1024];
                    while (true)
                    {
                        int c = stream.Read(buffer, 0, buffer.Length);
                        if (c == 0) break;
                        context.Response.OutputStream.Write(buffer, 0, c);
                    }
                }
                finally
                {
                    stream.Close();
                }
            }
        }

        bool IHttpHandler.IsReusable
        {
            get { return true; }
        }

   
    }
}