在目录浏览中,如果选择的是一个文件,单击“打开”按钮就可以进行文件下载。当然,也可以选择直接打开,如图1所示。
PRivate void BtnOpen_Click(object sender,
System.Event
args e)
{
if(FileList.Selected
ITem.Text
=="返回上级目录") //返回上级目录
{
string ParentPath=Directory.GetParent(
currentPath).ToString();
LoadDir(ParentPath);
return;
}
else if(FileList.SelectedItem.Text.IndexOf(".")>0) //打开文件
{
FileDownload(FileList.SelectedItem.Text);
}
else //打开目录
{
LoadDir(FileList.SelectedItem.Text);
}
}
private void FileDownload(string FullFileN
ame)
{
Fil
einfo Downloa
DFile = new FileInfo(YourFileName); //设置要下载的文件
Response.Clear(); //清除缓冲区流中的所有内容输出
Response.ClearHeaders(); //清除缓冲区流中的所有头
Response.Buffer = false; //设置缓冲输出为false
//设置输出流的 HTTP MIME 类型为application/
octet-stream
Response.ContentType = "application/octet-stream";
//将 HTTP 头添加到输出流
Response.AppendHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName,System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
//将指定的文件直接写入 HTTP 内容输出流。
Response.WriteFile(DownloadFile.FullName);
Response.Flush(); //向客户端发送当前所有缓冲的输出
Response.End(); //将当前所有缓冲的输出发送到客户端
}
。如果换一种方式进行处理的话,可以在页面加一个HyperLink控件困难。在选择相应的文件时,使其链接到相应的路径。这里不再多说。