附件下载

来源:互联网 发布:淘宝宝贝图尺寸 编辑:程序博客网 时间:2024/04/29 13:29

if (Request.QueryString["filepath"] != null)
        {
            string path = Request.QueryString["filepath"].ToString();
            FileInfo fi = new FileInfo(path);//获取文件路径
            string name = Request.QueryString["filename"];//获取附件原名
            if (fi.Exists)//判断文件是否存在
            {
                //将文件保存到本机上
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(name));
                Response.AddHeader("Content-Length", fi.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.Filter.Close();
                Response.WriteFile(fi.FullName);
                Response.End();
            }
            else
            {
                pubfun.Alert(Page, "您要下载的文件不存在!");
                pubfun.RegisterStartupScript(Page, "PAGE", "window.location.href='" + "DownList.aspx?ChannelId=9';");
            }
        }
        else
        {
            pubfun.Alert(Page, "请选择您要下载的文件!");
            pubfun.RegisterStartupScript(Page, "PAGE", "window.location.href='" + "DownList.aspx?ChannelId=9';");
        }