【转载】C# 从服务器下载文件
一、//TransmitFile实现下载
protected void Button1_Click(object sender, EventArgs e)
{
/*
微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite
下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。
代码如下:
*/
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=z.zip");
string filename = Server.MapPath("DownLoad/z.zip");
Response.TransmitFile(filename);
} 二、//WriteFile实现下载
protected void Button2_Click(object sender, EventArgs e)
{
/*
using System.IO; */
string fileName = "asd.txt";//客户端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.txt");//路径 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();
} 三、 //WriteFile分块下载
protected void Button3_Click(object sender, EventArgs e)
{
string fileName = "aaa.txt";//客户端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.txt");//路径 System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath); if (fileInfo.Exists == true)
{
const long ChunkSize = ;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
byte[] buffer = new byte[ChunkSize]; Response.Clear();
System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
long dataLengthToRead = iStream.Length;//获取下载的文件总大小
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
while (dataLengthToRead > && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, , Convert.ToInt32(ChunkSize));//读取的大小
Response.OutputStream.Write(buffer, , lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
Response.Close();
}
} 四、//流方式下载
protected void Button4_Click(object sender, EventArgs e)
{
string fileName = "aaa.txt";//客户端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.txt");//路径 //以字符流的形式下载文件
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, , bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
} //---------------------------------------------------------- public void DownloadFile( System.Web.UI.Page WebForm,String FileNameWhenUserDownload ,String FileBody )
{ WebForm.Response.ClearHeaders();
WebForm.Response.Clear();
WebForm.Response.Expires = ;
WebForm.Response.Buffer = true;
WebForm.Response.AddHeader("Accept-Language", "zh-tw");
//'文件名称
WebForm.Response.AddHeader("content-disposition", "attachment; filename='"+System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8)+"'");
WebForm.Response.ContentType = "Application/octet-stream";
//'文件内容
WebForm.Response.Write(FileBody);//-----------
WebForm.Response.End();
} //上面这段代码是下载一个动态产生的文本文件,若这个文件已经存在于服务器端的实体路径,则可以通过下面的函数: public void DownloadFileByFilePath( System.Web.UI.Page WebForm,String FileNameWhenUserDownload ,String FilePath )
{
WebForm.Response.ClearHeaders();
WebForm.Response.Clear();
WebForm.Response.Expires = ;
WebForm.Response.Buffer = true;
WebForm.Response.AddHeader("Accept-Language", "zh-tw");
//文件名称
WebForm.Response.AddHeader("content-disposition", "attachment; filename='" + System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8) +"'" );
WebForm.Response.ContentType = "Application/octet-stream";
//文件内容
WebForm.Response.Write(System.IO.File.Rea}dAllBytes(FilePath));//---------
WebForm.Response.End(); }
【转载】C# 从服务器下载文件的更多相关文章
- 【FTP】C# System.Net.FtpClient库连接ftp服务器(下载文件)
如果自己单枪匹马写一个连接ftp服务器代码那是相当恐怖的(socket通信),有一个评价较高的dll库可以供我们使用. 那就是System.Net.FtpClient,链接地址:https://net ...
- (4)FTP服务器下载文件
上一篇中,我们提到了怎么从FTP服务器下载文件.现在来具体讲述一下. 首先是路径配置.. 所以此处我们需要一个app.config来设置路径. <?xml version="1.0&q ...
- Python 实现批量从不同的Linux服务器下载文件
基于Python实现批量从不同的Linux服务器下载文件 by:授客 QQ:1033553122 实现功能 1 测试环境 1 使用方法 1 1. 编辑配置文件conf/file_for_downl ...
- 从Linux服务器下载文件到本地命令
从Linux服务器下载文件夹到本地1.使用scp命令 scp /home/work/source.txt work@192.168.0.10:/home/work/ #把本地的source.txt文件 ...
- 从Linux服务器下载文件夹到本地
从Linux服务器下载文件夹到本地 1.使用scp命令 scp /home/work/source.txt work@192.168.0.10:/home/work/ #把本地的source.txt文 ...
- Java Web实现使用浏览器从服务器下载文件(后台)
Java Web实现 使用浏览器从服务器下载文件. 下面实现两种情况的下载,需求如下: 需求(一):1.用户在页面填写表单. 2.填写完成后,选择下载,将表单内容发往后台. 3.后台根据内容生产一个文 ...
- js从服务器下载文件
通常,将文件绝对路径url作为超链接<a>的链接地址href的值,点击<a>后,浏览器将会尝试请求文件资源,如果浏览器能够辨认文件类型,则将会以预设的打开方式直接打开下载的文件 ...
- C#从服务器下载文件到客户端源码
1.在window窗体加个button控件,双击进去
- C# 从服务器下载文件代码的几种方法
一.//TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一 ...
随机推荐
- EL表达式自定义函数
表达式语言除了可以使用基本的运算符外,还可以使用自定义函数.通过使用自定义函数,加强了表达式语言的功能. EL表达式函数,主要功能是完成对数据的修改,统一化格式: 步骤 1.开发函数处理类,处理类就是 ...
- UI-不常用控件 UIActivityIndicatorView、UIProgressView、UISegmentedControl、UIStepper、UISwitch、UITextView、UIAlertController
1 //UIActivityIndicatorView //小菊花,加载================================================================ ...
- NAVagationController
UINavigationController为导航控制器,在iOS里经常用到. 1.UINavigationController的结构组成 UINavigationController有Navigat ...
- 单项选择RadioButton和多项选择CheckBox的使用
在Android中,可以通过RadioButton和RadioGroup的组合来实现单项选择的效果.而多项选择则是通过CheckBox来实现的. 1.单项选择RadioButton 我们知道,一 ...
- Photon Cloud Networking: OnPhotonSerializeView Not Firing
Photon Cloud Networking: OnPhotonSerializeView Not Firing http://answers.unity3d.com/questions/31305 ...
- Python虚拟环境设置
Python2环境 首先,我们用pip安装virtualenv: pip3 install virtualenv 然后,假定我们要开发一个新的项目,需要一套独立的Python运行环境,可以这么做: 第 ...
- 关于for循环中是否需要缓存length值的个人总结
在JS性能优化中,有一个常见的小优化,即 // 不缓存 for (var i = 0; i < arr.length; i++) { ... } // 缓存 var len = arr.leng ...
- 深入理解java虚拟机-第八章
第8章 虚拟机字节码执行引擎 8.2 运行时栈帧结构 栈帧(Stack Frame)是用于支持虚拟机进行方法调用和方法执行的数据结构. 每一个栈帧包括了局部变量表.操作数栈.动态连接.方法返回地址和一 ...
- struts1和struts2原理解析
1.struts1和struts2 是2个完全不同的框架 其实struts2核心就是 webwork框架 struts1以ActionServlet作为核心控制器,由ActionServlet负责拦截 ...
- 使用阿里云Code进行版本控制并配置IDEA
1. 申请阿里code的账号,网址如下https://code.aliyun.com, 2. 申请完成之后,将账号信息发给项目负责人,由负责人加入项目中 3. 下载git,下载地址为 ...