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 ...
随机推荐
- sqlite3---终端操作
进入沙盒路径 创建stu.sql数据库 sqlite3 stu.sql 创建表 create table if not exists Student (id integer primary key a ...
- ZOJ3549 Little Keng(快速幂)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Little Keng Time Limit: 2 Seconds Me ...
- mysql 主从搭建
主要搭建步骤如下: 1.打开binlog,设置server_id 打开主库的--log-bin,并设置server_id 2.主库授权 --最好也在从库对主库授权 ...
- 自己写的简单的jQuery分页控件
因为是内部项目,需要分页控件,网上找了一大堆,给领导一看,都说不行,原因很简单,太复杂,领导就想要个简单点的,类似百度的分页,可是自己也没写过Jquery控件,硬着头皮找了些资料,写了这个分页控件,目 ...
- 阿里大鱼simplexmlelement object 取值PHP
SimpleXMLElement Object( [code] => 15 [msg] => Remote service error [sub_code] => ...
- PHPExcel用法有感
今日项目需求导出excel.我用最简单的header头方式导出了,但是需求部门退回了,说不满足他们的需求,需要我按照他们的模板来导出. 然后想到了PHPExcel,没用过,走了不少弯路. 1.去官网下 ...
- pfsense 2.2RC版本应用
为什么要上RC版本呢?因为华硕主板有一个RTL8111G驱动在2.15中还没有识别.... 公司双线WAN,一个PPPOE一个静态IP. 开了端口转发, 要求对不同的IP进行相关限速, 到达指定网站用 ...
- LeetCode_Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- netstat命令, netstat指令在windows和linux有什么不同
查看当前tcp监听端口[op@TIM html]$ netstat -nltp(Not all processes could be identified, non-owned process inf ...
- C(m,n)%P
program1 n!%P(P为质数) 我们发现n! mod P的计算过程是以P为周期的的,举例如下: n = 10, P = 3 n! = 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 ...