ASP.NET批量下载服务器端指定目录文件
//遍历服务器指定文件夹下的所有文件
string path = "uploads/Image/";
string serverPath = Server.MapPath(path); //创建临时文件夹
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");
/// <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(, the_rar.Length - );
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;
}
}
下载生成的RAR文件
/// <summary>
/// 下载生成的RAR文件
/// </summary>
private void DownloadRAR(string file)
{
FileInfo fileInfo = new FileInfo(file);
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(, file.LastIndexOf("\\"));
//删除临时目录下的所有文件
DeleteFiles(tempPath);
//删除空目录
Directory.Delete(tempPath);
Response.End();
}
/// <summary>
/// 删除临时目录下的所有文件
/// </summary>
/// <param name="tempPath">临时目录路径</param>
private void DeleteFiles(string tempPath)
{
DirectoryInfo directory = new DirectoryInfo(tempPath);
try
{
foreach (FileInfo file in directory.GetFiles())
{
if (file.Attributes.ToString().IndexOf("ReadOnly") != -)
{
file.Attributes = FileAttributes.Normal;
}
File.Delete(file.FullName);
}
}
catch (Exception)
{
throw;
}
}
ASP.NET批量下载服务器端指定目录文件的更多相关文章
- ASP.NET批量下载文件的方法
一.实现步骤 在用户操作界面,由用户选择需要下载的文件,系统根据所选文件,在服务器上创建用于存储所选文件的临时文件夹,将所选文件拷贝至临时文件夹.然后调用 RAR程序,对临时文件夹进行压缩,然后输出到 ...
- ASP.NET批量下载文件
一.实现步骤 在用户操作界面,由用户选择需要下载的文件,系统根据所选文件,在服务器上创建用于存储所选文件的临时文件夹,将所选文件拷贝至临时文件夹.然后调用 RAR程序,对临时文件夹进行压缩,然后输出到 ...
- python之对指定目录文件夹的批量重命名
python之对指定目录文件夹的批量重命名 import os,shutil,string dir = "/Users/lee0oo0/Documents/python/test" ...
- wget镜像网站并且下载到指定目录 2012-06-20 19:40:56
wget镜像网站并且下载到指定目录 2012-06-20 19:40:56 分类: Python/Ruby wget -r -p -np -k -P /tmp/ap http://www.exampl ...
- Python - 批量下载 IIS 共享的文件
1.说明 用 IIS 以WEB形式发布了本地文件夹,提供文件下载,并设置了访问权限:默认下载需要点击一个一个的下载,web界面如下: 3.脚本 执行脚本批量下载文件,会在当前目录创建文件夹,并压缩该文 ...
- java批量下载,将多文件打包成zip格式下载
现在的需求的: 根据产品族.产品类型,下载该产品族.产品类型下面的pic包: pic包是zip压缩文件: t_product表: 这些包以blob形式存在另一张表中: t_imagefile表: 现在 ...
- asp.net+批量下载附件
asp.net 下载文件几种方式 protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方 ...
- 下载GitHub指定目录的文件
使用网站 https://minhaskamal.github.io/DownGit/#/home
- Python批量重命名指定目录下文件的两种方法
#法一 import os path = "C://Python34//" for file in os.listdir(path): if os.path.isfile(os.p ...
随机推荐
- SQL从入门到基础–08 Union、Union all及案例
一.联合结果集 1. 简单的结果集联合: Select FNumber,FName,FAge from T_Employee union select FidCardNumber,FName,FAge ...
- ClustrMaps——访问者位置
ClustrMaps——访问者位置: http://www3.clustrmaps.com/zh/counter/maps.php?url=http://www.cnblogs.com/JuneZha ...
- test在博客中嵌入实例代码
// 其实很简单,只要把css.div.js代码直接写在里面就可以了.通过查看源代码就能看得很清楚了. 要注意的一点就是:如果div里没有任何内容,则它会被(博客网)删除掉,所以即使没有内容, ...
- lazy loading img 图片延迟加载
http://yunpan.cn/cVsjPW6dgbcsh (提取码:b5db)
- [Python 3.x 官方文档翻译]Whetting Your Appetite 欢迎您的使用
If you do much work on computers, eventually you find that there’s some task you’d like to automate. ...
- [原创]零基础R语言教程---第一课---认识R语言
教程的录制的确是折腾了一番,一连录了二十多遍,有时候激动的说错了字,有时候不知道下一句说啥.. 不过好在第一课已经搞定了,哈哈. 虽然内容现在看起来还有点简单, 不过牛b也是一个过程嘛. 我会坚持下去 ...
- Java基础语法学习(1)switch...case
switch...case的标准语法 switch(待选择的变量) { case 值1:语句1; break; case 值2:语句2: break; ....... case 值n:语句n; bre ...
- DatePickerDialog 控制只选择年月或年或月
etXyLevelDate.setOnTouchListener(selectDateTouchListener()); /** * @desc 选择日期操作 * @param @return * @ ...
- IIC总线协议---以存储芯片at24c64为例
IIC总线协议 前言:年前给老师做个红外抄表系统,,现在对当中用到的一些模块总结一下. 1.只有在总线空闲时才允许启动数据传送. 2.在数据传送过程中,当时钟线为高电平时,数据线必须保持稳定状态,不允 ...
- STL 查找vector容器中的指定对象:find()与find_if()算法
1 从vector容器中查找指定对象:find()算法 STL的通用算法find()和find_if()可以查找指定对象,参数1,即首iterator指着开始的位置,参数2,即次iterator指着停 ...