您的位置:首页 > 编程语言 > ASP

ASP.NET实现文件下载

2014-02-16 21:36 141 查看
转:http://blog.csdn.net/codeshark/article/details/2473664

方式一:TransmitFile实现下载。将指定的文件直接写入 HTTP 响应输出流,而不在内存中缓冲该文件。

protected void Button1_Click(object sender, EventArgs e)
protected void Button2_Click(object
[b] sender, EventArgs e)
protected void Button3_Click(object sender, EventArgs e)
protected void Button4_Click(object sender, EventArgs e)


{


string fileName = "CodeShark.zip";//客户端保存的文件名


string filePath = Server.MapPath("DownLoad/CodeShark.zip");//路径


//以字符流的形式下载文件


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();


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