(九)上传和下载模块(下载文件)
2018-03-28 20:27
176 查看
下载文件是从网络中获得文件资源的主要手段,在assp.net中没有提供下载文件的控件,但可以通过filestream流的方式和response对象的write()方法,编写下载函数实现下载文件的操作。
页面加载的时候,要将服务器里面的文件通过listbox展示出来。
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
string strfilepath = Server.MapPath("~/morefile/");//获取路径
DirectoryInfo dir = new DirectoryInfo(strfilepath);//创建目录对象
FileSystemInfo[] files = dir.GetFileSystemInfos();//获取该目录下的所有文件
ListItem items;
foreach (FileSystemInfo infofiles in files)
{
items = new ListItem();
items.Text = infofiles.Name;
items.Value = infofiles.FullName;
ListBox1.Items.Add(items);
}
}
然后,我们开始写下载
protected void Button1_Click(object sender, EventArgs e)
{
string road = "morefile/"+ListBox1.SelectedItem.Text;
try
{
string url = Server.MapPath(road);//获得绝对下载路径
FileInfo file = new FileInfo(url);//获得硬盘上该文件的详细信息
if (file.Exists)
{
Response.Clear();//清空response对象中的内容
//通过response对象执行下载文件的操作
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlDecode(file.Name));//HttpUtility.UrlDecode解决下载框的中文乱码
Response.AddHeader("Content-Length",file.Length.ToString());
Response.ContentType="application/application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();//结束response对象
Response.Flush();//刷新
Response.Clear();//清空
}
4000
else
{
Response.Write("文件不存在");
}
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
效果如下
这部分代码在down下,https://github.com/1126048156/upanddownfile.git
页面加载的时候,要将服务器里面的文件通过listbox展示出来。
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
string strfilepath = Server.MapPath("~/morefile/");//获取路径
DirectoryInfo dir = new DirectoryInfo(strfilepath);//创建目录对象
FileSystemInfo[] files = dir.GetFileSystemInfos();//获取该目录下的所有文件
ListItem items;
foreach (FileSystemInfo infofiles in files)
{
items = new ListItem();
items.Text = infofiles.Name;
items.Value = infofiles.FullName;
ListBox1.Items.Add(items);
}
}
然后,我们开始写下载
protected void Button1_Click(object sender, EventArgs e)
{
string road = "morefile/"+ListBox1.SelectedItem.Text;
try
{
string url = Server.MapPath(road);//获得绝对下载路径
FileInfo file = new FileInfo(url);//获得硬盘上该文件的详细信息
if (file.Exists)
{
Response.Clear();//清空response对象中的内容
//通过response对象执行下载文件的操作
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlDecode(file.Name));//HttpUtility.UrlDecode解决下载框的中文乱码
Response.AddHeader("Content-Length",file.Length.ToString());
Response.ContentType="application/application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();//结束response对象
Response.Flush();//刷新
Response.Clear();//清空
}
4000
else
{
Response.Write("文件不存在");
}
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
效果如下
这部分代码在down下,https://github.com/1126048156/upanddownfile.git
相关文章推荐
- 使用paramiko模块远程执行命令、上传文件和下载文件
- Python paramiko模块 实现 ssh远程执行命令 上传下载文件 堡垒机模式下的远程命令执行
- 文件一键上传、汉字转拼音、excel文件上传下载功能模块的实现
- 文件一键上传、汉字转拼音、excel文件上传下载功能模块的实现
- 文件一键上传、汉字转拼音、excel文件上传下载功能模块的实现
- 如何使用 paramiko 模块来实现远程执行OS命令和文件上传下载?
- 利用python fabric模块写的批量操作远程主机脚本(命令执行,上传、下载文件)
- (八)上传和下载模块(单文件和多文件上传)
- 使用paramiko模块远程登录并上传或下载文件
- 实用模块:Asp.net实现的树形管理界面(支持zip文件上传和zip文件下载)
- python paramiko模块实现sftp上传下载文件
- python模块学习之paramiko远程执行命令,文件上传、下载
- python模块学习之paramiko远程执行命令,文件上传、下载
- 文件一键上传、汉字转拼音、excel文件上传下载功能模块的实现
- ASP.NET中文件上传下载方法集合
- Linux_ftp_命令行下下载文件get与上传文件put的命令应用
- struts1文件上传和下载 连接 使用Struts1实现文件上传(二)
- Asp.net文件的上传与下载
- struts2学习笔记(11)——struts2文件上传与下载
- nodejs文件上传处理模块formidable