日期:2014-05-19  浏览次数:20418 次

DataGrid中使用链接下载,当下载文件是IE能直接读的,要强制下载,求一方法,C#代码。
描述:
C:\Inetpub\wwwroot\mdown\aa\   目录下有2个文件,(.ppt文件   ,xml文件各一)
将这2文件名显示在DataGrid网格中,并将文件名做成下载链接
当访问.aspx文件(C:\Inetpub\wwwroot\mdown   目录下)能通过文件名链接下载,对于IE可读的xml,强制下载而不显示

问题:
DataGrid链接中的xml文件怎么不读取而强制下载(注:按纽事件下的对xml文件强制下载我知道,但DataGrid链接的强制下载的代码该写在那里呢?)

附代码:
WebForm1.apsx:

<%@   Page   language= "c# "   Codebehind= "WebForm1.aspx.cs "   AutoEventWireup= "false "   Inherits= "mdown.WebForm1 "   %>
<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.0   Transitional//EN "   >
<HTML>
<HEAD>
<title> WebForm1 </title>
<meta   name= "GENERATOR "   Content= "Microsoft   Visual   Studio   .NET   7.1 ">
<meta   name= "CODE_LANGUAGE "   Content= "C# ">
<meta   name= "vs_defaultClientScript "   content= "JavaScript ">
<meta   name= "vs_targetSchema "   content= "http://schemas.microsoft.com/intellisense/ie5 ">
</HEAD>
<body   MS_POSITIONING= "GridLayout ">
<form   id= "Form1 "   method= "post "   runat= "server ">
<asp:datagrid   id= "DataGrid1 "   style= "Z-INDEX:   101;   LEFT:   56px;   POSITION:   absolute;   TOP:   56px "   runat= "server "
Width= "96px "   Height= "160px "   AutoGenerateColumns= "False ">
<Columns>
<asp:HyperLinkColumn   DataTextField= "name "   DataNavigateUrlField= "name "   DataNavigateUrlFormatString= "aa/{0} "> </asp:HyperLinkColumn>
</Columns>
</asp:datagrid>
</form>
</body>
</HTML>


WebForm1.cs

private   void   Page_Load(object   sender,   System.EventArgs   e)
{
//   在此处放置用户代码以初始化页面

//   在此处放置用户代码以初始化页面

System.IO.DirectoryInfo   dinfo   =   new   DirectoryInfo(Server.MapPath( "/mdown/aa/ "));
System.IO.FileInfo[]   fileinfos   =   dinfo.GetFiles();


DataTable   dt   =   new   DataTable();
dt.Columns.Add( "name ");

foreach(FileInfo   fi   in   fileinfos)
{
System.IO.FileInfo   fileinfo=   fi;

DataRow   dr   =   dt.NewRow();
dr[ "name "]   =   fileinfo.FullName.Substring(fileinfo.FullName.LastIndexOf( "\\ ")   +   1);
dt.Rows.Add(dr);

}

DataGrid1.DataSource   =   dt.DefaultView;
DataGrid1.DataBind();


}


------解决方案--------------------
用流进行下载
例:
System.IO.FileInfo file = new System.IO.FileInfo( "F:\\mp3\\mp3\\别哭我最爱的人.mp3 ");
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.Charset= "GB2312 ";
Response.ContentEncoding=System.Text.Encoding.UTF8;
Response.ContentType = "application/octet-stream ";

Response.AddHeader( "Content-Disposition ", &q