string fileName = "201096218686"; string p = "201096198974.pdf";//定义文件名 string filePath = Server.MapPath("DataFile/" + p);//转换为物理路径 Response.ClearContent();//清空缓冲区内容 Response.ClearHeaders();//清空缓冲区HTTP头 string FilePost = filePath.Substring(filePath.Length - 3).ToLower();//取扩展名 switch (FilePost) { case "pdf"://PDF文件 Response.ContentType = "application/PDF";//设置HTTP头为PDF文档其它类似 break; case "doc": Response.ContentType = "application/msword"; break; case "xls": Response.ContentType = "application/vnd.ms-excel"; break; default: Session["ErrorInfo"] = "不支持的文件格式:" + FilePost; Response.Redirect("ErrorPage.aspx"); break; } Response.WriteFile(filePath);//写入客户端 Response.Flush();//客户更新 Response.Close();//写入关闭 Session.Remove("Report");//移除"Report"Session
在线阅读文件
最新推荐文章于 2014-03-26 13:15:02 发布
本文介绍了一段代码实现过程,该代码负责处理服务器上的文件并根据文件类型设置正确的HTTP响应头,支持PDF、DOC及XLS等格式,对于不支持的文件格式则导向错误页面。
1934

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



