局限性: 1、不适合数据交换,里面有html代码,比较乱,没有固定格式 2、datagrid不能有分页、排序等,否则出错 实现方法: Response.Clear(); ???Response.Buffer= false; ???Response.Charset="GB2312"; ???Response.AppendHeader("Content-Disposition","attachment;filename=test.xls"); ???Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312"); ???Response.ContentType = "application/ms-excel"; ???this.EnableViewState = false; ???System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); ???System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); ???this.DataGrid1.RenderControl(oHtmlTextWriter); ???Response.Write(oStringWriter.ToString()); ???Response.End();
ASP.NET直接下?一个文件,而不是在IE中打?它
.??各位大侠,用以下?句:
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "attachment;FileName=" + name);
byte[] fileBuffer = new byte[fileSize];
fileStream.Read(fileBuffer, 0, (int)fileSize);
context.Response.BinaryWrite(fileBuffer);
context.Response.End();
======================================
FileStream fileStream=new FileStream("C:\\doc\\test.doc",FileMode.Open);
long fileSize = fileStream.Length;
Context.Response.ContentType="application/octet-stream";
Context.Response.AddHeader("Content-Disposition","attachment; filename=\"" + HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8) + "\"");
Context.Response.AddHeader("Content-Length",fileSize.ToString());
byte[] fileBuffer=new byte[fileSize];
fileStream.Read(fileBuffer, 0, (int)fileSize);
fileStream.Close();
Context.Response.BinaryWrite(fileBuffer);
Context.Response.End();
============================================
int id = int.Parse(context.Request.QueryString["id"]);
string sendUserNo = context.Request.QueryString["sendUserNo"];
string receiveUserNo = context.Request.QueryString["receiveUserNo"];
Message message = new Message(id, sendUserNo, receiveUserNo);
string name = context.Server.UrlEncode(message.FileName);
string oppositeName = message.Filepath;
string absoluteName = context.Server.MapPath(message.Filepath);
FileStream fileStream = new FileStream(absoluteName, FileMode.Open,FileAccess.Read);
long fileSize = fileStream.Length;
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "attachment;FileName=" + name);
byte[] fileBuffer = new byte[fileSize];
fileStream.Read(fileBuffer, 0, (int)fileSize);
context.Response.BinaryWrite(fileBuffer);
context.Response.End();