string path = Server.MapPath(Url.Content("~/") + "UploadFiles/Template/");
FileStream fs = new FileStream(path + file, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
if (Request.UserAgent.ToLower().IndexOf("firefox") > -1) //火狐浏览器
{
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + file + "\"");
}
else //其他浏览器
{
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file, System.Text.Encoding.UTF8));
}
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
转载于:https://www.cnblogs.com/workstation-nigoudongma/p/9883577.html
本文展示了一个使用ASP.NET进行文件下载的代码示例,包括如何读取服务器上的文件并将其作为二进制流发送到客户端,同时设置响应头以支持不同浏览器的下载需求。
983

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



