您的位置:首页 > 其它

从服务器下载文件方法的实际应用

2018-03-12 09:37 246 查看
1、前台

<asp:Button ID="btndownfilebystream" runat="server" Text="附件下载" OnClick="btndownfilebystream_Click" />
2、后台
    protected void btndownfilebystream_Click(object sender, EventArgs e)
        {
            string L_id = Request["L_id"];
            string fileName = new DA.FeedbackManage.FeedbackManage().GetfileName(L_id);  //获取文件名的方法
            string filePath = Server.MapPath("~/Filepath/" + fileName);//路径//以字符流的形式下载文件
            FileStream fs = new FileStream(filePath, FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: