.NET中如何实现在页面中的word文档通过Button按钮下载?
解决方案 »
- 怎么查找两个txtbox中的相同的字符
- 求助,web视频播放器的问题!
- TreeView控件的TreeNodePopulate事件能触发几次?
- 分页翻页功能需点击两次"下一页"才能正常翻页,为什么?
- 为什么现在很多.NET 2.0虚拟主机不支持验证控件呢?
- 请教datagrid的问题,谁解决了再放100分
- 关于水晶报表导出execl的问题,导出之后列标题怎么是坚的导出的,跟原本的网页的格式正好的转了90度呀
- 100分求“毕业论文“写作思路,请各位高手指导,谢了!
- 高分 50 简单问题
- 为什么我的机器无法运行asp.net
- 本地计算机上的SQL2005数据库,为什么本地的VS2005内测试连接就连接不上?
- 自定义Web控件
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="center">
<asp:Button ID="cmdOpen" runat="server" Text="在线打开" CommandName="open" OnCommand="Button_Click" />
<asp:Button ID="cmdSave" runat="server" Text="本地保存" CommandName="save" OnCommand="Button_Click" />
<asp:DropDownList ID="listType" runat="server" >
<asp:ListItem Value="excel">Excel</asp:ListItem>
<asp:ListItem Value="word">Word</asp:ListItem>
</asp:DropDownList><br />
<br />
数据源:<br />
<br />
<asp:GridView ID="myGW" runat="server" >
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FillGridView();
}
private void OutPut(string fileType, string strType)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", fileType);
Response.ContentType = strType;
this.EnableViewState = false;
System.IO.StringWriter swOut = new System.IO.StringWriter();
HtmlTextWriter hTw = new HtmlTextWriter(swOut);
myGW.RenderControl(hTw);
Response.Write(swOut.ToString());
Response.End();
}
protected void Button_Click(object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "save":
switch (listType.SelectedValue)
{
case "excel":
OutPut("attachment;filename=out.xls", "application/ms-excel");
break;
case "word":
OutPut("attachment;filename=out.doc", "application/ms-word");
break;
}
break;
case "open":
switch (listType.SelectedValue)
{
case "excel":
OutPut("online;filename=out.xls", "application/ms-excel");
break;
case "word":
OutPut("online;filename=out.doc", "application/ms-word");
break;
}
break;
}
}
public override void VerifyRenderingInServerForm(Control control)
{
}
private void FillGridView()
{
string strConn = "server=.;database=sbskweb;user id=sa;password=sa";
SqlConnection conn = new SqlConnection(strConn);
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
string strSql = "SELECT top 10 序号=Rowid,父节点=FatherID,模块名称=Categoryname FROM Web_Category";
comm.CommandText = strSql;
conn.Open();
this.myGW.DataSource = comm.ExecuteReader();
this.myGW.DataBind();
conn.Close();
}
}
string filePath = Server.MapPath("");//路径 FileInfo fileInfo = new FileInfo(filePath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();
Response.End();
浏览器默认是可以直接打开图片和文本文件的,flash等媒体文件是需要浏览器插件机制的。
通过学习http协议后,有了一定理解。对于word文档等文件,默认请求时会弹出“文件另存为”的下载框。
当在IIS中配置了MIME类型关系映射后,可以关联某个文件类型与程序软件,访问该资源后会默认使用该软件打开。
例如,可是设置关联“*.abs”文件为媒体,访问该资源的时候windows会默认使用meadia player打开。
设置“*.doc”文件与“msword”程序相关联,访问该类型的资源时,就会默认使用msword打开
在IIS中可以直接配置,使用Tomcat服务器的话,在其安装目录下的web.xml文件中配置
-->