Code
//下载图片
protected void lbtnUpLoad_Click(object sender, EventArgs e)
{
int fileID = 0;
if (int.TryParse(Request.QueryString["FileID"], out fileID))
{
Files wFile = BLLManage<Files>.GetModel("fileid=" + fileID, "RealName,Path");
string f_name = wFile.RealName;
string f_path = wFile.Path;
string strRootPath = Explorer.Web.Utility.Directory.GetUserDomainPhysicalPath(this.User.OperateHome.HomeDomain);
System.IO.FileInfo file = new FileInfo(strRootPath + f_path);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename="+f_name );
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
Response.Flush();
Response.Clear();
}
}
//下载图片
protected void lbtnUpLoad_Click(object sender, EventArgs e)
{
int fileID = 0;
if (int.TryParse(Request.QueryString["FileID"], out fileID))
{
Files wFile = BLLManage<Files>.GetModel("fileid=" + fileID, "RealName,Path");
string f_name = wFile.RealName;
string f_path = wFile.Path;
string strRootPath = Explorer.Web.Utility.Directory.GetUserDomainPhysicalPath(this.User.OperateHome.HomeDomain);
System.IO.FileInfo file = new FileInfo(strRootPath + f_path);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename="+f_name );
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
Response.Flush();
Response.Clear();
}
}
Demo:下载有数据的Excel文档
Code
protected void btnExportDate_Click(object sender, EventArgs e)
{
Response.Clear();
//attachment 参数表示作为附件下载,可以改成online在线打开
//filename=FileFlow.xls 制定输出文件的名称,注意其扩展名和制定文件类型相符,可以为:.doc .xls .txt .htm
Response.AppendHeader("Content-Disposition", "attachment;filename=EOL_" + DateTime.Now.Ticks + ".xls");
//编码格式
//Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.ContentEncoding = System.Text.Encoding.UTF8;
//Response.ContentType制定文件类型,可以为:application/ms-excel,application/ms-word ,application/ms-txt,application/ms-html或其他浏览器可直接支持的文档
Response.ContentType = "application/ms-excel";
gvwOut.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
//将目标数据绑定到输入流输出
//this 表示输出本页,也可以绑定GridView,或其他支持 obj.RenderControl() 属性的控件
gvwOut.RenderControl(oHtmlTextWriter);
//this.DataBind.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
protected void btnExportDate_Click(object sender, EventArgs e)
{
Response.Clear();
//attachment 参数表示作为附件下载,可以改成online在线打开
//filename=FileFlow.xls 制定输出文件的名称,注意其扩展名和制定文件类型相符,可以为:.doc .xls .txt .htm
Response.AppendHeader("Content-Disposition", "attachment;filename=EOL_" + DateTime.Now.Ticks + ".xls");
//编码格式
//Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.ContentEncoding = System.Text.Encoding.UTF8;
//Response.ContentType制定文件类型,可以为:application/ms-excel,application/ms-word ,application/ms-txt,application/ms-html或其他浏览器可直接支持的文档
Response.ContentType = "application/ms-excel";
gvwOut.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
//将目标数据绑定到输入流输出
//this 表示输出本页,也可以绑定GridView,或其他支持 obj.RenderControl() 属性的控件
gvwOut.RenderControl(oHtmlTextWriter);
//this.DataBind.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}