>

NET批量下载文件

- 编辑:金沙国际平台登录 -

NET批量下载文件

一、达成步骤

  在客商操作分界面,由客商挑选必要下载的文件,系统基于所选文件,在服务器上成立用于存款和储蓄所选文件的有时文件夹,将所选文件拷贝至有的时候文件夹。然后调用 RA奥迪Q3程序,对有的时候文件夹实行削减,然后输出到客商端。最终删除临时文件夹。

二、代码已毕

1、ASP.NET批量下载 大旨代码C# 代码 复制

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

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

C# 代码 复制

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

3、DownloadRAR(string file)方法

C# 代码 复制

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

4、DeleteFiles(stringtempPath) 方法

C# 代码 复制

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

本文由编程发布,转载请注明来源:NET批量下载文件