在IE浏览器中打开WORD、EXCEL、PDF和TXT文件
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!IsPostBack)
4 {
5 string fname = Server.MapPath(".") + @"\Files\FarPointOne.doc";
6 Response.ClearContent( );
7 Response.ClearHeaders( );
8 Response.AppendHeader("Content-Disposition", string.Format("inline;filename={0}",fname));
9 Response.AppendHeader("content-type", "application/msword");
10 Response.WriteFile(fname);
11 Response.Flush( );
12 Response.Close( );
13 }
14 }
关于这段代码,有一点要注意:content-type 的取值
content-type: application/pdf (PDF文件) || application/msword(WORD文件) || application/x-msexcel(EXCEL文件) || text/plain (文本文件)
如果要在IE中打开的是PDF或者TXT格式的文件,这段代码直接就能实现。但是当选择打开的是EXCEL或者WORD文件时,就弹出的是下载对话框了。
这个问题,让我纠结了十多分钟,到底是哪里的问题呢?是 content-type有误?不会啊,我找了好多网址,都是这个。
于是继续搜索资料,综合搜索资料和实验结果,得出如下解决方案。如下是设置步骤的截图:
图中所示的是WORD文件的设置方式,对于EXCEL文件只需要找到扩展名为 XLS和XLSX的项,并做同样的设置就OK。
注意:
* Content-Disposition的可能取值: * p_w_upload 表示作下载 * inline 表示在浏览器中打开
转自:http://blog.sina.com.cn/s/blog_67cc72cc01016kdt.html
转载于:https://blog.51cto.com/zhaoyingyatou/1610932