FileStream fileStream = null;
byte[] bytes = null;
fileStream = new FileStream(name, FileMode.Open, FileAccess.Read);
bytes = new byte[(int)fileStream.Length];
fileStream.Read(bytes, 0, (int)fileStream.Length);
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name));
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.End();
http://www.topso.net/
本文介绍了一种使用 C# 实现文件下载的方法。通过 FileStream 和 HttpContext 对象读取文件内容并发送到客户端,设置正确的 Content-Type 和 Content-Disposition 头来指定文件以附件形式下载。
2万+

被折叠的 条评论
为什么被折叠?



