void down(string filepath)
{
//确定文件的物理路径
string filePath = Server.MapPath(filepath);
FileInfo Fi = new FileInfo(filePath);
if (Fi.Exists)
{
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(Fi.Name, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
else
{
Response.Write("<script>alert('文件不存在!');</script>");
}
}
.net 文件点击下载函数 记录
最新推荐文章于 2019-10-08 22:12:43 发布
本文介绍了一种使用ASP.NET实现文件下载的方法。具体步骤包括获取指定路径下的文件、检查文件是否存在、读取文件内容并设置HTTP响应头以触发浏览器下载等。如果文件不存在,则会提示用户。
3万+

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



