Ejemplo n.º 1
0
        public void ProcessRequest(HttpContext context)
        {
            string fileName = context.Server.HtmlDecode(context.Request.QueryString["fileFullName"]);

            try
            {
                var speed = 0;
                Int32.TryParse(ConfigurationManager.AppSettings["DownloadSpeed"], out speed);

                if (File.Exists(fileName))
                {
                    DownLoad.DownloadFile(context, fileName, speed * 1024);
                }
                else
                {
                    context.Response.Write("<script  language='javascript'>window.alert('待下载的文件被移动或删除!');window.close(); </script>");
                }
            }
            catch (Exception)
            {
                context.Response.Write("<script  language='javascript'>window.alert('下载文件时发生异常!');window.close(); </script>");
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/javascript";
            try
            {
                Stopwatch watch = new Stopwatch();
                watch.Start();
                string multiDownRptParams = context.Server.HtmlDecode(context.Request.QueryString["multiDownRptParams"]);
                var    rptIdArray         = multiDownRptParams.Split('@');
                var    fileUrlArray       = new List <string>();
                foreach (var rptId in rptIdArray)
                {
                    string json = MultiDelReport.HttpGet(context, rptId);
                    if (json == "[]" || json == null || json == "null" || json == string.Empty)
                    {
                        continue;
                    }
                    IList <ReportInfo> jsonArray = JsonConvert.DeserializeObject <IList <ReportInfo> >(json);
                    var data       = jsonArray[0];
                    var Status     = data.status;
                    var downRptUrl = string.Empty;
                    if (Status == "0")
                    {
                        downRptUrl = data.UnconfirmedUrl;
                    }
                    else
                    {
                        downRptUrl = data.ConfirmedUrl;
                    }
                    if (File.Exists(downRptUrl))
                    {
                        fileUrlArray.Add(downRptUrl);
                    }
                }
                //剔除重复的元素
                fileUrlArray = fileUrlArray.Distinct().ToList();
                if (!fileUrlArray.Any())
                {
                    context.Response.ContentType = "text/html";
                    context.Response.Write("<script  language='javascript'>window.alert('待下载的文件被移动或删除!');window.close(); </script>");
                }
                else
                {
                    Stopwatch watch2 = new Stopwatch();
                    watch2.Start();
                    string rootPath       = Path.GetTempPath();
                    string zipFileName    = "report_" + (DateTime.Now.Ticks / 10000);
                    string tempDirectory  = rootPath + "\\" + zipFileName; //压缩目录
                    string zipFullName    = tempDirectory + ".zip";        //压缩文件全名
                    string downloadAsName = "监测报表.zip";

                    if (Directory.Exists(tempDirectory))
                    {
                        Directory.Delete(tempDirectory, true);
                    }
                    Directory.CreateDirectory(tempDirectory);
                    foreach (var fileName in fileUrlArray)
                    {
                        var zipFileFullName = tempDirectory + "\\" + new FileInfo(fileName).Name;
                        if (File.Exists(fileName))
                        {
                            File.Copy(fileName, zipFileFullName);
                        }
                    }
                    watch2.Stop();
                    string time2 = watch2.ElapsedMilliseconds.ToString();
                    logger.Debug(string.Format("拷贝文件..总耗时..{0} 毫秒\r\n", time2));
                    Stopwatch watch3 = new Stopwatch();
                    watch3.Start();
                    CreateZip(tempDirectory, zipFullName);
                    watch3.Stop();
                    string time3 = watch3.ElapsedMilliseconds.ToString();
                    logger.Debug(string.Format("压缩文件..总耗时..{0} 毫秒\r\n", time3));
                    var tempPathDir = new DirectoryInfo(tempDirectory);
                    tempPathDir.Delete(true);
                    Stopwatch watch4 = new Stopwatch();
                    watch4.Start();
                    //var speed = 1*1024*1024; //1M
                    var speed = 0;
                    Int32.TryParse(ConfigurationManager.AppSettings["MultiDownloadSpeed"], out speed);
                    DownLoad.DownloadFile(context, zipFullName, speed * 1024 * 1024, downloadAsName);
                    watch4.Stop();
                    string time4 = watch4.ElapsedMilliseconds.ToString();
                    logger.Debug(string.Format("下载文件..总耗时..{0} 毫秒\r\n", time4));
                    watch.Stop();
                    string time = watch.ElapsedMilliseconds.ToString();
                    logger.Debug(string.Format("批量下载..总耗时..{0} 毫秒\r\n", time));
                    //删除临时文件
                    var zipFile = new FileInfo(zipFullName);
                    zipFile.Delete();
                }
            }
            catch (Exception)
            {
                context.Response.ContentType = "text/html";
                context.Response.Write("<script  language='javascript'>window.alert('下载文件时发生异常!');window.history.go(-2);window.opener.location.reload(); " + "</ " + "script>");
            }
        }