下载文件夹的解决方案

摘要:
string rarName){string the_rar;RegistryKeythe_Reg;string the_Info;Processthe_Process;try{the_Reg=Registry.ClassesRoot.OpenSubKey(@“WinRAR”);the _rar.Length-7);

ASP.NET批量下载文件的方法

这篇文章主要介绍了ASP.NET批量下载文件的方法,实例汇总了常见的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");

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;

    }

}

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();

}

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;

    }

}

JavaScript:

引入头

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>donw2-多文件演示页面</title>

    <link type="text/css" href="http://t.zoukankan.com/js/down.css" rel="Stylesheet" />

    <script type="text/javascript" src="http://t.zoukankan.com/js/jquery-1.4.min.js"></script>

    <script type="text/javascript" src="http://t.zoukankan.com/js/down.app.js" charset="utf-8"></script>

    <script type="text/javascript" src="http://t.zoukankan.com/js/down.edge.js" charset="utf-8"></script>

    <script type="text/javascript" src="http://t.zoukankan.com/js/down.file.js" charset="utf-8"></script>

    <script type="text/javascript" src="http://t.zoukankan.com/js/down.folder.js" charset="utf-8"></script>

    <script type="text/javascript" src="http://t.zoukankan.com/js/down.js" charset="utf-8"></script>

</head>

创建down2对象

var downer = new DownloaderMgr();

downer.Config["Folder"] = "";//设置默认下载路径。

//挂载事件

downer.event.taskCreate = function (obj) { $(document.body).append("文件ID:" + obj.fileSvr.id) + "<br/>"; };

downer.event.downProcess = function (obj) { };

downer.event.downStoped = function (obj) { };

downer.event.downComplete = function (obj) {

    $(document.body).append('<div>本地路径:' + obj.fileSvr.pathLoc + '</div>');

};

downer.event.downError = function (obj, err) { };

downer.event.queueComplete = function () { $(document.body).append("<div>队列完成</div>"); };

批量下载url

$("#btn-down-files").click(function () {

    if (downer.Config["Folder"] == "") { downer.open_folder(); return; }

    var urls = [

        { fileUrl: "http://res2.ncmem.com/res/images/ie11.png" }

        , { fileUrl: "http://res2.ncmem.com/res/images/up6.1/down.png" }

        , { fileUrl: "http://res2.ncmem.com/res/images/firefox.png" }

        , { fileUrl: "http://res2.ncmem.com/res/images/edge.png" }

        , { fileUrl: "http://res2.ncmem.com/res/images/up6.1/cloud.png" }

        , { fileUrl: "http://res2.ncmem.com/res/images/home/w.png" }

        , { fileUrl: "http://res2.ncmem.com/res/images/img.png" }

    ];

    downer.app.addUrls(urls);

});

当成一个文件夹下载

$("#btn-down-json").click(function () {

    if (downer.Config["Folder"] == "") { downer.open_folder(); return; }

    var fd = {

        nameLoc: "图片列表"

        , files: [

            { fileUrl: "http://res2.ncmem.com/res/images/ie11.png" }

            , { fileUrl: "http://res2.ncmem.com/res/images/up6.1/down.png" }

            , { fileUrl: "http://res2.ncmem.com/res/images/firefox.png" }

            , { fileUrl: "http://res2.ncmem.com/res/images/edge.png" }

            , { fileUrl: "http://res2.ncmem.com/res/images/up6.1/cloud.png" }

            , { fileUrl: "http://res2.ncmem.com/res/images/home/w.png" }

            , { fileUrl: "http://res2.ncmem.com/res/images/img.png" }

        ]

    };

    downer.app.addJson(fd);

});

下载多级目录

$("#btn-down-fd").click(function () {

    if (downer.Config["Folder"] == "") { downer.open_folder(); return; }

    var fd = {

        nameLoc: "测试文件夹"

        , files: [

            { fileUrl: "http://www.ncmem.com/images/ico-ftp.jpg" }

            , { fileUrl: "http://www.ncmem.com/images/ico-up.jpg" }

        ]

        , folders: [

            {

                nameLoc: "图片1"

                , files: [

                    { fileUrl: "http://www.ncmem.com/images/ico-ftp.jpg" }

                    , { fileUrl: "http://www.ncmem.com/images/ico-up.jpg" }

                    , { fileUrl: "http://www.ncmem.com/images/ico-capture.jpg" }

                    , { fileUrl: "http://www.ncmem.com/images/ico-imageuploader.gif" }

                    , { fileUrl: "http://www.ncmem.com/images/ico-wordpaster.gif" }

                ]

                , folders: [

                    {

                        nameLoc: "软件"

                        , files: [

                            { fileUrl: "http://res2.ncmem.com/res/images/edit-file.png" }

                        ]

                    }

                ]

            }

        ]

    };

    downer.app.addJson(fd);

});

自定义下载文件名称

$("#btn-down-svr").click(function () {

    if (downer.Config["Folder"] == "") { downer.open_folder(); return; }

    var urls = [

        { fileUrl: "http://localhost:90/db/down.aspx", nameLoc: "test.exe" }

        , { fileUrl: "http://localhost:90/db/down.aspx", nameLoc: "test-1.exe" }

    ];

    downer.app.addUrls(urls);

});

最终实现效果:

下载文件夹的解决方案第1张

希望本文所述对大家的asp.net#程序设计有所帮助。

网上示例:http://blog.ncmem.com/wordpress/2019/08/28/net文件批量下载/

免责声明:文章转载自《下载文件夹的解决方案》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇SQLALCHEMY_TRACK_MODIFICATIONS警告解决方法为 setuptools 开路搭桥下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

记一次线上环境的内存溢出(java.lang.OutOfMemoryError)

事故背景 今天客户说风控项目有个别用户查询不到数据不是报错就是一直卡在那里,我就去那个接口看了下。 一看项目日志今天的都几个g了,平常也就几百兆吧,很明显出了问题。 请求接口后使用命令tail -f 实时查看日志,发现有个东西一个在刷屏,几分钟了还在刷。 把日志切割后查看还发现了堆内存溢出错误,使用命令 free -m 发现服务器4g内存几乎已经占满了。...

tomcat服务器的安装与配置

一、常见服务器 WebLogic(BEA)、webSphere(IBM)、Tomcat(Apache) 二、Tomcat的下载与安装 下载地址:https://tomcat.apache.org/ 安装目录不能包含中文和空格。 因运行tomcat需要JDK,所以请先配置好JDK的环境变量。 端口占用问题:netstat  -ano 命令查看端口占用信息 三...

Oracle字段转字符串/多行记录合并/连接/聚合字符串的几种方法

原文:http://czjxdm.iteye.com/blog/466948 Oracle字段转字符串/多行记录合并/连接/聚合字符串的几种方法 博客分类:技术文章 OracleSQL  怎么合并多行记录的字符串,一直是oracle新手喜欢问的SQL问题之一,关于这个问题的帖子我看过不下30个了,现在就对这个问题,进行一个总结。    什么是合并多...

使用Element的table合并单元格的问题(合并行)

目标样式:  官网案例: <el-table :data="tableData" :span-method="arraySpanMethod" border style=" 100%"> <el-table-column prop="id" label="I...

Linux下MySQL安装及配置

Linux下MySQL安装及配置 安装MySQL   Ubuntu系统中,直接使用apt install的方式去安装MySQL的服务端和客户端,MySQL的客户端必须安装,否则无法通过命令连接并操作MySQL。安装命令如下:   apt install -y mysql-server mysql-client   管理MySQL服务   启动、停止和重启服...

记CentOs服务器内存使用满的解决办法

使用的一个应用服务器,weblogic部署,nginx分配,在使用了半个月的时间里,服务器内存达到杀掉服务器进程之后查看被删除但是仍然被应用程序占用的文件列表由于我删掉了日志里所以东西,所以导致后来我又得使用了一个命令由于我删了日志里的ngnix.pid 但是我删除的只是结尾log日志呀,不知道怎么就把pid结尾文件删除了,奇怪反正就报这个错“/usr/l...