using System.IO;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) //首次加载
{
string[] AllTxt = Directory.GetFiles(Server.MapPath("File"));
foreach (string name in AllTxt)
{
//将文件名添加到ListBox中
ListBox1.Items.Add(Path.GetFileName(name));
}
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Session["select"] = ListBox1.SelectedValue.ToString();
}
protected void DownButton1_Click(object sender, ImageClickEventArgs e)
{
//判断是否选择文件名
if (ListBox1.SelectedValue != "")
{
if (Session["select"] != "")
{ //获取文件路径
string FilePath = Server.MapPath("File/") + Session["select"].ToString();
//初始化 FileInfo 类的实例,它作为文件路径的包装
FileInfo Filepath = new FileInfo(FilePath);
//判断文件是否存在
if (Filepath.Exists)
{
//将文件保存到本机上
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(Filepath.Name));
Response.AddHeader("Content-Length", Filepath.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Filter.Close();
Response.WriteFile(Filepath.FullName);
Response.End();
}
}
}
else
{
Page.RegisterStartupScript("sb", "<script>alert('请先选择文件名')</script>");
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。