下载超大文件的控件

一、实现步骤

 

在用户操作界面,由用户选择需要下载的文件,系统根据所选文件,在服务器上创建用于存储所选文件的临时文件夹,将所选文件拷贝至临时文件夹。然后调用 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");

 

2、RARSave(string tempFolder, string tempName) 方法

代码如下:

///

/// 生成RAR文件

///

/// 存放复制文件的目录

/// RAR文件存放目录

/// RAR文件名

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)方法

代码如下:

///

/// 下载生成的RAR文件

///

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) 方法

代码如下:

///

/// 删除临时目录下的所有文件

///

/// 临时目录路径

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;

}

}

详细配置与控件信息可以参考我写的这篇文章:http://blog.ncmem.com/wordpress/2019/08/28/net%E6%96%87%E4%BB%B6%E6%89%B9%E9%87%8F%E4%B8%8B%E8%BD%BD/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
由于OA系统是基于WEB方式的,如果只判断用户是否点击“下载文件”,就来判断用户是否成功下载文件。这种方式很显然行不通,怎么办呢?我想到的就是用一控件,然后用回调事件来处理数据库方面的问题(下载记录问题) 有兴趣的朋友可在 http://www.interdrp.com/ 下载分销系统 用测试帐号进系统后,点测试程序,再点WEB下载 可以看见效果 https://www.interdrp.com/software/ReYoWebDownLoad.zip(点击下载控件) API说明 ReYoWebDL.copyright="锐洋软件拥有版权 http://www.interdrp.com/" //必须 ReYoWebDL.url="http://dl.baofeng.com/storm3/Storm2009-0504-1.exe" //下载文件的路径 ReYoWebDL.path ="" //保存文件地址 ReYoWebDL.ReYoStartDownload() //下载动作 ReYoWebDL.ReYoStopDownload() //停止下载 ReYoWebDL.size //下载文件大小 ReYoWebDL.bytes//已下载大小 ReYoWebDL.speed //下载速度 KB/S ReYoWebDL.done //下载是否完成 ReYoWebDL.cancle=true; //是否取消下载 ReYoWebDL.urlsource //下载文件名 ReYoWebDL.percent //下载百分比 升级提示: 20090506:新增剩余时间,下载百分比进度,下载速度及文件保存位置显示;且支持断点续传功能。 20090504:新增下载过程序取消下载动作。 20090502:新增文件存在提示。 目前控件支持两种方式下载: 1:开发者在程序中指定下载文件的url,保存文件的path。例如:download("http://www.interdrp.com/software/hotel/setup.zip","c:\a.zip" 2:开发者在程序中指定下载文件的url,不指定保存文件的path,系统会提示您选择保存文件的路径。例如:download("http://www.interdrp.com/software/hotel/setup.zip",""
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值