`
流星剑
  • 浏览: 91811 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

文件下载的实现

    博客分类:
  • .net
 
阅读更多
 /// <summary>
    /// 文件下载
    /// </summary>
    protected void downF()
    {
            string fileName= ((Label)DetailsView1.FindControl("Label1")).Text.ToString();
            if (fileName != "")
            {   //获取文件路径
                string path = Server.MapPath("../Files/") +fileName;
                //初始化 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("", "<script>alert('资源不存在!')</script>");
        }
    }

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics