<%@ WebHandler Language="C#" Class="DownloadHandler" %>

using System;
using System.Web;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web.Script.Serialization;
using System.IO;
using System.Net;
using System.Web.Services;
using System.Text;
using BasicData;
using DataLinkMesWork;

using MESDownloadExcel;
public class DownloadHandler : IHttpHandler
{
    /// <summary>
    /// 00 标准DataTable
    /// 01 计划工时模板
    /// 02 外协加工任务单
    /// 03 外购备料请款
    /// 04 自家项目报表
    /// 05 材料清单
    /// </summary>
    /// <param name="context"></param>
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";
        var data = context.Request;
        var stream = new StreamReader(data.InputStream).ReadToEnd();

        //JsonData jsonData;
        //string responseText = "NULL";
        byte[] bytes = null;
        string fileName = "test.xsl";

        try
        {
            string InvoiceNum = HttpContext.Current.Request["param"];

            JsonData jsonData = JsonMapper.ToObject(InvoiceNum);
            DataLink.LogJsonData(jsonData);

            MESDownloadExcel.ExcelWebCall.ExcelFile(jsonData,out bytes,out fileName);
            fileName = fileName + ".xls";

            HttpContext.Current.Response.ContentType = "application/octet-stream";
            //HttpContext.Current.Response.ContentType = getContentType(extension);  
            //通知浏览器下载文件而不是打开  
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            HttpContext.Current.Response.AddHeader("Access-Control-Expose-Headers", "Content-Disposition");
            HttpContext.Current.Response.BinaryWrite(bytes);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
            return;
        }
        catch (Exception ex)
        {
            return;
        }


    }

    ///// <summary>
    ///// Unicode编码(汉字转换为\uxxx)
    ///// </summary>
    ///// <param name="str"></param>
    ///// <returns></returns>
    //public static string EnUnicode(string str)
    //{
    //    StringBuilder strResult = new StringBuilder();
    //    if (!string.IsNullOrEmpty(str))
    //    {
    //        for (int i = 0; i < str.Length; i++)
    //        {
    //            strResult.Append("\\u");
    //            strResult.Append(((int)str[i]).ToString("x"));
    //        }
    //    }
    //    return strResult.ToString();
    //}

    ///// <summary>
    ///// Unicode解码（\uxxxx转换为汉字）
    ///// </summary>
    ///// <param name="str"></param>
    ///// <returns></returns>
    //public static string DeUnicode(string str)
    //{
    //    //最直接的方法Regex.Unescape(str);
    //    Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");
    //    return reg.Replace(str, delegate(Match m) { return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString(); });
    //}

    //             /// <summary>
    ///// Unicode解码
    ///// </summary>
    ///// <param name="str"></param>
    ///// <returns></returns>
    //public static string DeUnicode(string str)
    //{
    //    //最直接的方法Regex.Unescape(str);
    //    Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");
    //    return reg.Replace(str, delegate (Match m) { return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString(); });
    //}

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