得知互动

标题: asp.net获取服务器指定文件夹目录文件并提供下载的方法 [打印本页]

作者: swmozowtfl    时间: 2015-7-31 22:04
标题: asp.net获取服务器指定文件夹目录文件并提供下载的方法
本文实例讲述了asp.net获取服务器指定文件夹目录文件并提供下载的方法。分享给大家供大家参考。具体实现方法如下:
复制代码 代码如下:string dirpath = httpcontext.current.server.mappath(uploads/);
if (directory.exists(dirpath))
{
//获得目录信息
directoryinfo dir = new directoryinfo(dirpath);
//获得目录文件列表
fileinfo[] files = dir.getfiles(*.*);
string[] filenames = new string[files.length];
//临时数据表
datatable dt = new datatable();
dt.columns.add(filename);

foreach (fileinfo fileinfo in files)
{
datarow dr = dt.newrow();
dr[filename] = fileinfo.name;
dt.rows.add(dr);
}
repeater1.datasource = dt;
repeater1.databind();
}
if (e.commandname == down)
{
try
{
string downloadfilename = ~/uploads/ + e.commandargument.tostring();//文件路径
string filepath = server.mappath(downloadfilename);
string filename = path.getfilename(filepath);
fileinfo file = new fileinfo(filepath);
response.clear();
response.contenttype = application/octet-stream;
response.addheader(content-disposition, attachment; filename= + httputility.urlencode(filename, system.text.encoding.utf8));
response.addheader(content-length, file.length.tostring());
response.flush();
response.writefile(filepath);
}
catch
{
response.write(<script>alert('没有找到下载的源文件')</script>);
}
}
希望本文所述对大家的asp.net程序设计有所帮助。

更多网页制作信息请查看: 网页制作
作者: Acropozelan    时间: 2015-10-31 22:30
我是来收集资料滴...
作者: seazvyt    时间: 2015-10-31 22:30
这个站一周前刚刚有了点起色
作者: tohme    时间: 2015-10-31 22:31
@,@..是什么意思呀?
作者: alapScady    时间: 2016-3-13 21:29
要不我崇拜你?行吗?
作者: gevaemaidovef    时间: 2016-3-13 21:29
哈哈 怎么说来眼睛小真的很好 哈 哈哈~~~~
作者: Mqokjdvq    时间: 2016-3-13 21:29
哎 怎么说那~~
作者: mwxny    时间: 2016-3-13 21:30
今天统计好像出了问题
作者: effoggikeftor    时间: 2016-3-13 21:30
我去看看 网络上的事 我不是很会 哎




欢迎光临 得知互动 (https://bbs.dezhifl.com/) Powered by Discuz! X3.4