Exemple #1
0
        public void ProcessRequest(HttpContext context)
        {
            Stopwatch watch = new Stopwatch();

            watch.Start();
            string multiUploadRptParams = context.Server.HtmlDecode(context.Request.QueryString["MultiUploadRptParams"]);
            var    multiUploadRpt       = multiUploadRptParams.Split('@');

            context.Response.ContentType = "text/html";
            HttpFileCollection files = HttpContext.Current.Request.Files;
            var retArray             = new List <string>();

            try
            {
                for (int i = 0; i < multiUploadRpt.Count(); i++)
                {
                    var    RptId = multiUploadRpt[i];
                    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 RptUrl = string.Empty;
                    if (Status == "0")
                    {
                        RptUrl = data.UnconfirmedUrl;
                    }
                    else
                    {
                        RptUrl = data.ConfirmedUrl;
                    }
                    string fileName = new FileInfo(RptUrl).Name;
                    var    now      = DateTime.Now;
                    var    filePath = string.Empty;
                    if (Status == "2")
                    {
                        filePath = manualFileRootPath;
                    }
                    else
                    {
                        filePath = fileRootPath + now.Year.ToString() + "\\" + string.Format("{0:D2}", now.Month) + "\\" +
                                   string.Format("{0:D2}", now.Day) + "\\";
                    }
                    var fileFullName = Path.Combine(filePath, fileName);
                    var dirRet       = UploadHandler.CreateDirectory(filePath);
                    if (dirRet)
                    {
                        files[i].SaveAs(fileFullName);
                        var ret = UpdateFileInfoToDb(context, RptId, fileFullName, Status);
                        retArray.Add(ret);
                    }
                }
                var result = "[";
                for (int i = 0; i < retArray.Count; i++)
                {
                    if (i > 0)
                    {
                        result += ",";
                        result += retArray[i];
                    }
                    else
                    {
                        result += retArray[i];
                    }
                }
                result += "]";
                watch.Stop();
                string time = watch.ElapsedMilliseconds.ToString();
                logger.Debug(string.Format("批量上传..总耗时..{0} 毫秒\r\n", time));
                context.Response.Write(result);
            }
            catch (WebException e)
            {
                if ((int)((HttpWebResponse)e.Response).StatusCode == 405)
                {
                    context.Response.StatusCode = 202;
                    context.Response.Write(405);
                    context.Response.End();
                }
                else
                {
                    throw e;
                }
            }
            catch (Exception e)
            {
                context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                context.Response.Write(e.Message);
            }
        }
        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>");
            }
        }