ASP.NET 文件打包下载
2014-04-18 17:31
387 查看
ASP.NET 文件打包下载
用到ZipClass 文件压缩类 参考 http://blog.csdn.net/t_t_x_s/article/details/24017713
protected void Button1_Click(object sender, EventArgs e)
{
//下载文件
string path = Server.MapPath("xlt/");
string copyPath = Server.MapPath("Zip/xlt/");
FileInfo fi = new FileInfo(path + "ajaxMethod使用方法.txt");
int i=1;
while (Directory.Exists(copyPath))
{
copyPath = Server.MapPath("Zip/xlt" + i + "/");
i++;
}
Directory.CreateDirectory(copyPath);
if (fi.Exists) {
fi.CopyTo(copyPath+fi.Name);
}
fi = new FileInfo(path + "恶趣味.xlsx");
if (fi.Exists)
{
fi.CopyTo(copyPath + fi.Name);
}
fi = new FileInfo(path + "清恶趣味.xls");
if (fi.Exists)
{
fi.CopyTo(copyPath + fi.Name);
}
ZipClass zc = new ZipClass();
zc.ZipFileMain(copyPath,Server.MapPath("Zip/xlt.zip"),"");
// DirectoryInfo zipPath = new DirectoryInfo(copyPath);
// zipPath.Delete(); //删除文件夹
//删除文件夹
DeleteFolder(copyPath);
Response.Clear(); //解决txt中出现乱码
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode("xlt.zip"));//解决文件名中文乱码
string filename = Server.MapPath("Zip/xlt.zip");
Response.TransmitFile(filename);
Response.End(); //解决txt中出现乱码
fi = new FileInfo(Server.MapPath("Zip/xlt.zip"));
fi.Delete(); //删除压缩包
}
//删除文件夹
private void DeleteFolder(string folder){
if (Directory.Exists(folder))
{
foreach (string filepath in Directory.GetFileSystemEntries(folder))
{
//循环删除文件
if (File.Exists(filepath))
{
File.Delete(filepath);
}
//递归删除子文件夹
else {
DeleteFolder(filepath);
}
}
Directory.Delete(folder); //删除空文件夹
}
else {
throw new Exception("文件夹 " + folder + "不存在!");
}
}
用到ZipClass 文件压缩类 参考 http://blog.csdn.net/t_t_x_s/article/details/24017713
protected void Button1_Click(object sender, EventArgs e)
{
//下载文件
string path = Server.MapPath("xlt/");
string copyPath = Server.MapPath("Zip/xlt/");
FileInfo fi = new FileInfo(path + "ajaxMethod使用方法.txt");
int i=1;
while (Directory.Exists(copyPath))
{
copyPath = Server.MapPath("Zip/xlt" + i + "/");
i++;
}
Directory.CreateDirectory(copyPath);
if (fi.Exists) {
fi.CopyTo(copyPath+fi.Name);
}
fi = new FileInfo(path + "恶趣味.xlsx");
if (fi.Exists)
{
fi.CopyTo(copyPath + fi.Name);
}
fi = new FileInfo(path + "清恶趣味.xls");
if (fi.Exists)
{
fi.CopyTo(copyPath + fi.Name);
}
ZipClass zc = new ZipClass();
zc.ZipFileMain(copyPath,Server.MapPath("Zip/xlt.zip"),"");
// DirectoryInfo zipPath = new DirectoryInfo(copyPath);
// zipPath.Delete(); //删除文件夹
//删除文件夹
DeleteFolder(copyPath);
Response.Clear(); //解决txt中出现乱码
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode("xlt.zip"));//解决文件名中文乱码
string filename = Server.MapPath("Zip/xlt.zip");
Response.TransmitFile(filename);
Response.End(); //解决txt中出现乱码
fi = new FileInfo(Server.MapPath("Zip/xlt.zip"));
fi.Delete(); //删除压缩包
}
//删除文件夹
private void DeleteFolder(string folder){
if (Directory.Exists(folder))
{
foreach (string filepath in Directory.GetFileSystemEntries(folder))
{
//循环删除文件
if (File.Exists(filepath))
{
File.Delete(filepath);
}
//递归删除子文件夹
else {
DeleteFolder(filepath);
}
}
Directory.Delete(folder); //删除空文件夹
}
else {
throw new Exception("文件夹 " + folder + "不存在!");
}
}
相关文章推荐
- ASP.NET : 如何将服务端的多个文件打包下载
- ASP.NET多文件批量打包下载
- ASP.NET : 如何将服务端的多个文件打包下载
- Asp.Net Web Api 2 实现多文件打包并下载文件示例源码_转
- ASP.NET Web Api 2实现多文件打包并下载文件的实例
- ASP.NET : 如何将服务端的多个文件打包下载
- ASP.NET多文件批量打包下载
- ASP.NET多文件批量打包下载 (这个好)
- ASP.NET多文件批量打包下载
- ASP.NET多文件批量打包下载
- ASP.NET : 如何将服务端的多个文件打包下载
- ASP.NET Web Api 2实现多文件打包并下载文件的实例
- ASP.NET多文件批量打包下载 (这个好)
- ASP.NET多文件批量打包下载
- asp.net多文件打包下载
- ASP.NET : 如何将服务端的多个文件打包下载
- ASP.NET : 如何将服务端的多个文件打包下载
- ASP.NET多文件批量打包下载
- ASP.NET 打包下载文件
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)