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>");
}
}
转载于:https://www.cnblogs.com/ccsbb/archive/2011/04/07/2007984.html
本文介绍了一种使用ASP.NET实现文件下载的方法。通过定义voiddown函数处理文件路径,并检查文件是否存在。若文件存在,则创建文件流并读取文件内容,设置响应类型为下载,确保文件以附件形式提供给用户。
3万+

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



