asp.net批量下载图片
问题:
我想实现的是一个一个的下载。
比如我有一个文件列表。通过checkbox选择。通过单击下载按钮下载选中文件。
百度到都是用打包形式实现批量下载。
这是我自己写的代码,但是点击下载后只能下载一个文件。单步调试,循环只执行一次。不知道是什么原因。
string Name;
string Path;
protected void Button2_Click(object sender, EventArgs e)
{
foreach (Control control in Panel1.Controls)
{
try
{
if (((CheckBox)control).Checked)
{
//获取选择文件的相对路径。我将文件的相对路径写在了CssClass里面
Path = ((CheckBox)control).CssClass;
string[] path = Path.Split(‘/’);
Name = path[path.Length – 1];
download(Path, Name);
}
}
catch (Exception)
{ continue; }
}
}
private void download(string Path, string Name)
{
//用户下载文件时保存文件使用的默认命名
string downLoadFileName = Name;
//设定响应类型为下载
this.Response.ContentType = “application/x-zip-compressed”;
//获取下载文件在服务器端的物理路径
string downLoadPath = this.Server.MapPath(Path);
//添加响应头标示信息 this.Server.UrlPathEncode对下载文件名进行编码 防止文件名乱码
this.Response.AddHeader(“Content-Disposition”, string.Format(“attachment;filename={0}”, this.Server.UrlPathEncode(downLoadFileName)));
//下载文件输出
this.Response.TransmitFile(downLoadPath);
this.Response.End();
}
解决方案:
使用泽优软件下载控件(down2)来实现
JavaScript:
引入头
<head>
<metahttp-equiv=”Content-Type”content=”text/html; charset=utf-8″/>
<title>donw2-多文件演示页面</title>
<linktype=”text/css”href=”js/down.css”rel=”Stylesheet”/>
<scripttype=”text/javascript”src=”js/jquery-1.4.min.js”></script>
<scripttype=”text/javascript”src=”js/down.app.js”charset=”utf-8″></script>
<scripttype=”text/javascript”src=”js/down.edge.js”charset=”utf-8″></script>
<scripttype=”text/javascript”src=”js/down.file.js”charset=”utf-8″></script>
<scripttype=”text/javascript”src=”js/down.folder.js”charset=”utf-8″></script>
<scripttype=”text/javascript”src=”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);
});
实现效果:
希望本文所述对大家的asp.net#程序设计有所帮助。
网上示例:http://blog.ncmem.com/wordpress/2019/08/28/net文件批量下载/