多种下载文件方式 Response.BinaryWrite(byte[] DocContent);Response.WriteFile(System.IO.FileInfo DownloadFile .FullName);Response.Write(string html2Excel);
通过html给xls赋值,并下载xls文件
一、this.Response.Write(sw.ToString());System.IO.StringWriter sw = new System.IO.StringWriter();
this.Response.Clear();
string strFileName;
strFileName = "报表" + ".xls";
Response.Buffer = true;
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Accept-Language", "zh-cn");
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode(strFileName));
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(sw);
this.gvMutiQueResult.AllowPaging = false;
if (Session["QueryResult"] != null)
{
this.gvMutiQueResult.DataSource = (DataTable)Session["QueryResult"];
this.gvMutiQueResult.DataBind();
}
else
{
BindGV(); 绑定数据
}
this.gvMutiQueResult.RenderControl(htmlWrite);
this.Response.Write(sw.ToString());
this.Response.End();
Response.Write("<script type='text/javascript'>window.history.back();</script>");
二、Response.Write(html2Excel);string html2Excel
string strHtml; string fileName;
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(fileName) + ".xls\"");
Response.ContentType = "application/ms-excel";
string html2Excel = "<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\"><head><meta content=\"text/html;charset=UTF-8\" http-epuiv=\"content-type\">"
+
string.Format(@"<!--[if gte mso 9]><xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>{0}</x:Name>
<x:WorksheetOptions>
<x:Print>
<x:ValidPrinterInfo />
</x:Print>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook>
</xml>
<![endif]-->", fileName)
+ "</head><body>"
+ strHtml +
"</body></html>";
Response.Write(html2Excel);
Response.End();
三、
//获取数据
IDataBase sDB = DBFactory.GetDBInstance();
DataTable dt = sDB.GetDataTable("select * from 表 where ID='" + ID.ToString() + "'");
//输出
if (dt.Rows.Count > 0)
{
string DocFileName = dt.Rows[0]["DocFileName"].ToString();
string DocSize = dt.Rows[0]["DocSize"].ToString();
byte[] DocContent = (byte[])dt.Rows[0]["DocContent"];
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
//string ContentType = dt.Rows[0]["DocType"].ToString();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(DocFileName, System.Text.Encoding.UTF8) + "\"");
Response.AddHeader("Content-Length", DocSize.ToString());
Response.BinaryWrite(DocContent);
Response.Flush();
Response.End();
}
四、Response.WriteFile(DownloadFile.FullName); System.IO.FileInfo DownloadFile = new System.IO.FileInfo(strFileName);
//下载开机提醒安装程序 rar
private void DownloadDocument()
{
try
{
string strFileName = Server.MapPath("../../help/BootAlert.rar");
System.IO.FileInfo DownloadFile = new System.IO.FileInfo(strFileName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/ms-excel";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}
catch (Exception err)
{
throw err;
}
}
五、 byte[] binaryContent Response.BinaryWrite(binaryContent);
newFileName = newFileName.Replace("\\\\", "\\").Replace("\\", "/");
string outputFileName = newFileName.Substring(newFileName.LastIndexOf('/') + 1);
byte[] binaryContent = System.IO.File.ReadAllBytes(newFileName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(outputFileName, System.Text.Encoding.UTF8) + "\"");
Response.AddHeader("Content-Length", binaryContent.Count().ToString());
Response.BinaryWrite(binaryContent);
Response.Flush();
Response.End();
六、Response.TransmitFile(strFilePath, 0, fileSize);long fileSize = info.Length
FileInfo info = new FileInfo(strFilePath);
long fileSize = info.Length;
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Accept-Language", "zh-cn");
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode(info.Name));
//不指明Content-Length用Flush的话不会显示下载进度
Response.AddHeader("Content-Length", fileSize.ToString());
Response.TransmitFile(strFilePath, 0, fileSize);
Response.Flush();
Response.Close();
info.Delete();
多种下载文件方式 Response.BinaryWrite(byte[] DocContent);Response.WriteFile(System.IO.FileInfo DownloadFile .FullName);Response.Write(string html2Excel);的更多相关文章
- 文件读写(一)利用File静态类 System.IO.FileInfo、DirectoryInfo、DriveInfo
提供用于创建.复制.删除.移动和打开单一文件的静态方法,并协助创建 FileStream 对象. 一.读文件: 1.返回字符串:File.ReadAllText() string readText = ...
- MVC下载文件方式
MVC下载文件方式 http://www.cnblogs.com/liang--liang/archive/2012/10/20/2732745.html 方式一: public FileStream ...
- MVC下载文件方式 包括网络地址文件
MVC下载文件方式 方式一: public FileStreamResult DownFile(string filePath, string fileName){ string absol ...
- ASP.NET 下载文件方式
protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法TransmitFile来解决使 ...
- 向linux服务器上传下载文件方式收集
向linux服务器上传下载文件方式收集 1. scp [优点]简单方便,安全可靠:支持限速参数[缺点]不支持排除目录[用法] scp就是secure copy,是用来进行远程文件拷贝的.数据传输使用 ...
- java中多种写文件方式的效率对比实验
一.实验背景 最近在考虑一个问题:“如果快速地向文件中写入数据”,java提供了多种文件写入的方式,效率上各有异同,基本上可以分为如下三大类:字节流输出.字符流输出.内存文件映射输出.前两种又可以分为 ...
- C#下载文件,Stream 和 byte[] 之间的转换
stream byte 等各类转换 http://www.cnblogs.com/warioland/archive/2012/03/06/2381355.html using (System.Net ...
- .net 直接输出远程文件到浏览器和下载文件保存到本机
利用了xmlhttp,实现代码比较简单具体实现如下: 首先bin文件引入,com->microsoft xml v3.0 具体代码如下: protected void Button1_Click ...
- .net 下载文件几种方式
方式一:TransmitFile实现下载.将指定的文件直接写入 HTTP 响应输出流,而不在内存中缓冲该文件. protected void Button1_Click(object sender, ...
随机推荐
- C++中多重继承构造函数执行顺序
代码1: #include <cstdio> #include <iostream> using namespace std; class A{ public: A(){ co ...
- shell脚本实现查找文件夹下重复的文件,并提供删除功能
Windows下有软件FindDupFile,可以搜索指定目录及其下子目录,列出所有内容完全相同的文件(文件名可能不同),然后由用户选择删除重复的文件. 然而shell脚本却可以使用几行的命令完成与此 ...
- php魔术方法——属性重载方法
php有一类很神奇的方法,这些方法是保留方法,通常不会在外部被显式调用,他们使用双下划线(__)开头,他们被称为魔术方法(Magic Methods).php官方也不建议定义其他双下划线开头的方法. ...
- SalutJs
SalutJs 前言 卤煮在公司之初接触到的是一个微信APP应用.前端技术采用的是Backbone+zepto等小型JS类库.在项目开发之初,这类中小型的项目采用这两种库可以满足基本的需求.然而,随着 ...
- 模拟Hibernate动态生成SQL语句
这里有一个xml配置文件,也就是Hibernate框架中会用到的POJO和数据库的映射文件 <?xml version="1.0" encoding="utf-8& ...
- [HDU] 2063 过山车(二分图最大匹配)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2063 女生为X集合,男生为Y集合,求二分图最大匹配数即可. #include<cstdio> ...
- 直接修改别人jar包里面的class文件 工具:jclasslib
出于某些原因 需要把别人jar包里面的class修改一下信息 配置文件*.properties MANIFEST.MF 这些东西可以直接用记事本打开修改 然后替换掉就OK.. 在网上游荡了半天,没有找 ...
- Linux企业级项目实践之网络爬虫(7)——DNS解析
DNS 是Domain Name Service的缩写.域名系统为Internet上的主机分配域名地址和IP地址.IP地址不易于记忆,然而域名地址相比较而言是方便于记忆的.用户如果使用域名地址,当想获 ...
- 【转】Install SmartGit via PPA in Ubuntu 13.10/13.04/12.04/Linux Mint
原文网址:http://ubuntuhandbook.org/index.php/2013/09/install-smartgit-via-ppa-ubuntu-linux-mint/ This tu ...
- 40 个超棒的免费 Bootstrap HTML5 网站模板
Bootstrap 是快速开发Web应用程序的前端工具包.它是一个CSS和HTML的集合,它使用了最新的浏览器技术,给你的Web开发提供了时尚的版式,表单,buttons,表格,网格系统等等. 目前 ...