ASP.NET批量下载文件的方法
程序员文章站
2024-02-19 21:25:22
本文实例讲述了asp.net批量下载文件的方法。分享给大家供大家参考。具体方法如下:
一、实现步骤
在用户操作界面,由用户选择需要下载的文件,系统根据所选文件,在服务器...
本文实例讲述了asp.net批量下载文件的方法。分享给大家供大家参考。具体方法如下:
一、实现步骤
在用户操作界面,由用户选择需要下载的文件,系统根据所选文件,在服务器上创建用于存储所选文件的临时文件夹,将所选文件拷贝至临时文件夹。然后调用 rar程序,对临时文件夹进行压缩,然后输出到客户端。最后删除临时文件夹。
二、代码实现
1、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");
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");
2、rarsave(string tempfolder, string tempname) 方法
复制代码 代码如下:
/// <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;
}
}
/// 生成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)方法
复制代码 代码如下:
/// <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(0, file.lastindexof("\\\\"));
//删除临时目录下的所有文件
deletefiles(temppath);
//删除空目录
directory.delete(temppath);
response.end();
}
/// 下载生成的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(0, file.lastindexof("\\\\"));
//删除临时目录下的所有文件
deletefiles(temppath);
//删除空目录
directory.delete(temppath);
response.end();
}
4、deletefiles(string temppath) 方法
复制代码 代码如下:
/// <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") != -1)
{
file.attributes = fileattributes.normal;
}
file.delete(file.fullname);
}
}
catch (exception)
{
throw;
}
}
/// 删除临时目录下的所有文件
/// </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") != -1)
{
file.attributes = fileattributes.normal;
}
file.delete(file.fullname);
}
}
catch (exception)
{
throw;
}
}
希望本文所述对大家的asp.net#程序设计有所帮助。