如果要在IE中打开的是PDF,excel或者doc格式的文件,这段代码直接就能实现。
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 (文本文件).
我在文件下载的部分 需要在具体文件名上加超链接 到可以直接在浏览器打开 /** * 下载Action * @return */ public String downloadKnowledge() throws Exception{ checkFormat(download); return SUCCESS; } /** * 检查格式,文件后缀 * @param fileName */ private void checkFormat(String fileName){ String[] splitString = fileName.split("\\."); int length = splitString.length; String format = null; if (length > 1){ format = splitString[length - 1]; } DataFormat df = Enum.valueOf(DataFormat.class, format); switch (df){ case xls: // contentType设定 contentType = "application/vnd.ms-excel;charset=utf-8"; // attachment表示网页会出现保存、打开对话框 filename = "inline; filename=" + fileName; break; case xlsx: // contentType设定 contentType = "application/vnd.ms-excel;charset=utf-8"; // attachment表示网页会出现保存、打开对话框 filename = "inline; filename=" + fileName; break; case pdf: // contentType设定 contentType = "application/pdf;charset=utf-8"; // attachment表示网页会出现保存、打开对话框 filename = "inline; filename=" + fileName; break; case doc: // contentType设定 contentType = "application/msword;charset=utf-8"; // attachment表示网页会出现保存、打开对话框 filename = "inline; filename=" + fileName; break; case docx: // contentType设定 contentType = "application/msword;charset=utf-8"; // attachment表示网页会出现保存、打开对话框 filename = "inline; filename=" + fileName; break; case txt: // contentType设定 contentType = "text/plain;charset=utf-8"; // attachment表示网页会出现保存、打开对话框 filename = "inline; filename=" + fileName; break; default: // contentType设定 contentType = "text/plain;charset=utf-8"; // attachment表示网页会出现保存、打开对话框 filename = "inline; filename=" + fileName; break; } } 应该和你说的差不多啊 为什么我的总是不行
解决方法很简bai单,步骤是这样的,打开“我的电脑”,在菜单里选择“du工具”->“文件夹选项”,会弹出对话框。在对话框里选择“文件类型”这zhi个属性页,在列表中选中扩展名为daodoc的类型,按下面的“高级”按钮,在弹出的回“编辑文件类型”对话框里答钩上“下载后确认打开”复选框就可以了。