码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET批量下载文件

时间:2014-11-06 14:21:24      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   ar   os   for   sp   strong   文件   

一、实现步骤


  在用户操作界面,由用户选择需要下载的文件,系统根据所选文件,在服务器上创建用于存储所选文件的临时文件夹,将所选文件拷贝至临时文件夹。然后调用 RAR程序,对临时文件夹进行压缩,然后输出到客户端。最后删除临时文件夹。

 

二、代码实现

 
 
1、ASP.NET批量下载 核心代码
 
 
C# 代码   复制
bubuko.com,布布扣
bubuko.com,布布扣//遍历服务器指定文件夹下的所有文件
bubuko.com,布布扣            string path = "uploads/Image/";
bubuko.com,布布扣            string serverPath = Server.MapPath(path);
bubuko.com,布布扣
bubuko.com,布布扣            //创建临时文件夹
bubuko.com,布布扣            string tempName = DateTime.Now.ToString("yyyyMMddHHMMss");
bubuko.com,布布扣            string tempFolder = Path.Combine(serverPath, tempName);
bubuko.com,布布扣            Directory.CreateDirectory(tempFolder);
bubuko.com,布布扣
bubuko.com,布布扣            DirectoryInfo folder = new DirectoryInfo(serverPath);
bubuko.com,布布扣            foreach (FileInfo file in folder.GetFiles())
bubuko.com,布布扣            {
bubuko.com,布布扣                string filename = file.Name;
bubuko.com,布布扣                File.Copy(serverPath + "/" + filename, tempFolder + "/" + filename);
bubuko.com,布布扣            }
bubuko.com,布布扣            //ZKHelper.JSHelper.Alert("图片拷贝成功!");
bubuko.com,布布扣            //产生RAR文件,及文件输出
bubuko.com,布布扣            RARSave(tempFolder, tempName);
bubuko.com,布布扣            DownloadRAR(tempFolder + "\\\\" + tempName + ".rar");
bubuko.com,布布扣

 

2、RARSave(string tempFolder, string tempName) 方法

 

 
C# 代码   复制
bubuko.com,布布扣
bubuko.com,布布扣/// <summary>
bubuko.com,布布扣        /// 生成RAR文件
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        /// <param name="path">存放复制文件的目录</param>
bubuko.com,布布扣        /// <param name="rarPatch">RAR文件存放目录</param>
bubuko.com,布布扣        /// <param name="rarName">RAR文件名</param>
bubuko.com,布布扣        private void RARSave(string rarPatch, string rarName)
bubuko.com,布布扣        {
bubuko.com,布布扣            string the_rar;
bubuko.com,布布扣            RegistryKey the_Reg;
bubuko.com,布布扣            Object the_Obj;
bubuko.com,布布扣            string the_Info;
bubuko.com,布布扣            ProcessStartInfo the_StartInfo;
bubuko.com,布布扣            Process the_Process;
bubuko.com,布布扣            try
bubuko.com,布布扣            {
bubuko.com,布布扣                the_Reg = Registry.ClassesRoot.OpenSubKey(@"WinRAR");
bubuko.com,布布扣                the_Obj = the_Reg.GetValue("");
bubuko.com,布布扣                the_rar = the_Obj.ToString();
bubuko.com,布布扣                the_Reg.Close();
bubuko.com,布布扣                the_rar = the_rar.Substring(1, the_rar.Length - 7);
bubuko.com,布布扣                the_Info = " a " + rarName + " -r";
bubuko.com,布布扣                the_StartInfo = new ProcessStartInfo();
bubuko.com,布布扣                the_StartInfo.FileName = "WinRar";//the_rar;
bubuko.com,布布扣                the_StartInfo.Arguments = the_Info;
bubuko.com,布布扣                the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
bubuko.com,布布扣                //打包文件存放目录
bubuko.com,布布扣                the_StartInfo.WorkingDirectory = rarPatch;
bubuko.com,布布扣                the_Process = new Process();
bubuko.com,布布扣                the_Process.StartInfo = the_StartInfo;
bubuko.com,布布扣                the_Process.Start();
bubuko.com,布布扣                the_Process.WaitForExit();
bubuko.com,布布扣                the_Process.Close();
bubuko.com,布布扣            }
bubuko.com,布布扣            catch (Exception)
bubuko.com,布布扣            {
bubuko.com,布布扣                throw;
bubuko.com,布布扣            }
bubuko.com,布布扣        }
bubuko.com,布布扣

 

3、DownloadRAR(string file)方法

 

 
C# 代码   复制
bubuko.com,布布扣
bubuko.com,布布扣/// <summary>
bubuko.com,布布扣        /// 下载生成的RAR文件
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        private void DownloadRAR(string file)
bubuko.com,布布扣        {
bubuko.com,布布扣            FileInfo fileInfo = new FileInfo(file);
bubuko.com,布布扣            Response.Clear();
bubuko.com,布布扣            Response.ClearContent();
bubuko.com,布布扣            Response.ClearHeaders();
bubuko.com,布布扣            Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name);
bubuko.com,布布扣            Response.AddHeader("Content-Length", fileInfo.Length.ToString());
bubuko.com,布布扣            Response.AddHeader("Content-Transfer-Encoding", "binary");
bubuko.com,布布扣            Response.ContentType = "application/octet-stream";
bubuko.com,布布扣            Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
bubuko.com,布布扣            Response.WriteFile(fileInfo.FullName);
bubuko.com,布布扣            Response.Flush();
bubuko.com,布布扣            string tempPath = file.Substring(0, file.LastIndexOf("\\\\"));
bubuko.com,布布扣            //删除临时目录下的所有文件
bubuko.com,布布扣            DeleteFiles(tempPath);
bubuko.com,布布扣            //删除空目录
bubuko.com,布布扣            Directory.Delete(tempPath);
bubuko.com,布布扣            Response.End();
bubuko.com,布布扣        }
bubuko.com,布布扣


4、DeleteFiles(string tempPath) 方法

 

 
C# 代码   复制
bubuko.com,布布扣
bubuko.com,布布扣/// <summary>
bubuko.com,布布扣        /// 删除临时目录下的所有文件
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        /// <param name="tempPath">临时目录路径</param>
bubuko.com,布布扣        private void DeleteFiles(string tempPath)
bubuko.com,布布扣        {
bubuko.com,布布扣            DirectoryInfo directory = new DirectoryInfo(tempPath);
bubuko.com,布布扣            try
bubuko.com,布布扣            {
bubuko.com,布布扣                foreach (FileInfo file in directory.GetFiles())
bubuko.com,布布扣                {
bubuko.com,布布扣                    if (file.Attributes.ToString().IndexOf("ReadOnly") != -1)
bubuko.com,布布扣                    {
bubuko.com,布布扣                        file.Attributes = FileAttributes.Normal;
bubuko.com,布布扣                    }
bubuko.com,布布扣                    File.Delete(file.FullName);
bubuko.com,布布扣                }
bubuko.com,布布扣            }
bubuko.com,布布扣            catch (Exception)
bubuko.com,布布扣            {
bubuko.com,布布扣                throw;
bubuko.com,布布扣            }
bubuko.com,布布扣        }
bubuko.com,布布扣

ASP.NET批量下载文件

标签:style   http   io   ar   os   for   sp   strong   文件   

原文地址:http://www.cnblogs.com/ranran/p/4078497.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!