zoukankan      html  css  js  c++  java
  • 断点续传

    指定文件下载问题
    //首次加载页面方法
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)//首次加载
            {
                addListBox();   //调用用户自定义的addListBox方法            
            }
    }
    //被调用的自定义方法
      protected void addListBox()
        {
            //将指定文件夹中的文件保存到字符串数组中
            string[] name = Directory.GetFiles(Server.MapPath("File"));
            foreach (string s in name)
            {
                //将文件名添加到ListBox中
                LisBoxFile.Items.Add(Path.GetFileName(s));
            }     
        }//CodeGo.net/
    //实现ListBox控件赋值给Session变量中
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["txt"] = LisBoxFile.SelectedValue.ToString();//从ListBox控件中选择的项赋值给Session["txt"]中
        }
    //保存路径下载该文件
    protected void dFile()
        {
            //判断是否选择文件名
            if (LisBoxFile.SelectedValue != "")
            {
                if (Session["txt"] != "")
                {   //获取文件路径
                    string path = Server.MapPath("File/") + Session["txt"].ToString();
                    //初始化 FileInfo 类的实例,它作为文件路径的包装
                    FileInfo fi = new FileInfo(path);
                    
                    //判断文件是否存在
                    if (fi.Exists)
                    {
                        //将文件保存到本机上
                        Response.Clear();
                        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fi.Name));
                        Response.AddHeader("Content-Length", fi.Length.ToString());
                        Response.ContentType = "application/octet-stream";
                        Response.Filter.Close();
                        Response.WriteFile(fi.FullName);
                        Response.End();
                    }
                }
            }
            else
            {
                Page.RegisterStartupScript("sb", "<script>alert('请您先选择文件名')</script>");
            }
        }
    //指定下载到本地磁盘中
    protected void ImgBtnDownFile_Click(object sender, ImageClickEventArgs e)
        {
           dFile();//调用用户自定义的dFile方法,实现
        }
    //断点续传跳转按钮
    protected void ImgBtnUp_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("Default.aspx");// 跳转到文件下载
        }

  • 相关阅读:
    IOTest-InputStream-OutputStream
    JSP
    java链表
    区块链
    MySQL常用命令
    jQuery
    javascript
    Nginx
    Linux
    Hive
  • 原文地址:https://www.cnblogs.com/joean/p/4938085.html
Copyright © 2011-2022 走看看