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");// 跳转到文件下载
        }

  • 相关阅读:
    给C# 2005提供VB2005特有的My命名空间!
    利用iTextSharp把DataTable导出为PDF和RTF(Rich Text Format)文件
    [非技术][小孩]色色的小孩
    中秋节快乐!
    SnipperImages(Silverlight DEMO)控件设计之Slider和ColorSlider
    IE8的Activities,WebSlices示例ie8.taobao.com
    Net框架中的设计模式之Builder(构造者)兼谈抽象工厂和composite模式
    DiscuzNT 商品交易插件设计之[线下交易流程]
    听“汉代风云”,看“晁错之死”
    [翻译] python Tutorial 之一
  • 原文地址:https://www.cnblogs.com/joean/p/4938085.html
Copyright © 2011-2022 走看看