• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
翩翩
业精于勤,荒于嬉,行成于思,毁于随
博客园    首页    新随笔    联系   管理    订阅  订阅
C#.web 打开PDF

转自:http://blog.163.com/red_guitar@126/blog/static/11720612820112483221665/

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

posted on 2013-08-29 13:44  翩翩  阅读(3474)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3