您的位置:首页 > 编程语言 > ASP

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 + "不存在!");

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: