打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
文件下载代码
 
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>");
        }
    }
 
 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C# 从服务器下载文件代码
C# 文件上传类
python 获取当前目录下文件
QT读取文件夹下的特定文件_qt打开文件夹路径并读取文件
C# 文件路径、文件名、扩展名
ASP.NET上传文件的几种方法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服