c#文件下载示例的4种方法分享

 更新时间:2014年03月07日 14:29:56   作者:  
这篇文章主要介绍了c#文件下载示例的4种方法,有TransmitFile实现下载,WriteFile实现下载,WriteFile分块下载,流方式下载,需要的朋友可以参考下

复制代码 代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//TransmitFile实现下载
protected void Button1_Click(object sender, EventArgs e)
{
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)
{
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 = 102400;//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 > 0 && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
Response.OutputStream.Write(buffer, 0, 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, 0, 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();
}
}

相关文章

  • C#中的DataTable查询实战教程

    C#中的DataTable查询实战教程

    这篇文章主要介绍了C#中的DataTable查询实战教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • C#实现读取USB转串口参数并显示在ComboBox

    C#实现读取USB转串口参数并显示在ComboBox

    在很多应用程序中,尤其是那些需要与外部硬件通信的程序中,自动检测和读取串口参数是一个非常有用的功能,下面我们就来看看如何在C#中实现这一功能吧
    2024-01-01
  • 基于params,ref,out的参数问题详解

    基于params,ref,out的参数问题详解

    本篇文章是对params,ref,out的参数问题进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • macOS系统下Vscode的python配置教程

    macOS系统下Vscode的python配置教程

    这篇文章主要介绍了macOS系统下Vscode的python配置教程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04
  • C#将Word转换成PDF方法汇总(基于Office和WPS)

    C#将Word转换成PDF方法汇总(基于Office和WPS)

    这篇文章主要汇总了C#将Word转换成PDF方法,基于Office和WPS的两种解决方案,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • C#使用TimeSpan对象实现获取时间间隔

    C#使用TimeSpan对象实现获取时间间隔

    TimeSpan对象代表两个时间段的间隔或跨度,使用TimeSpan对象可以方便地获取两个时间段的间隔,下面我们就来看看C#使用TimeSpan对象实现获取时间间隔的具体操作吧
    2024-01-01
  • C#使用正则表达式

    C#使用正则表达式

    这篇文章介绍了C#使用正则表达式的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • C#托管堆对象实例包含内容分析

    C#托管堆对象实例包含内容分析

    这篇文章主要介绍了C#托管堆对象实例包含内容,实例展示了托管对象的结构及运行原理,需要的朋友可以参考下
    2014-09-09
  • C# PropertyInfo类案例详解

    C# PropertyInfo类案例详解

    这篇文章主要介绍了C# PropertyInfo类案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • C#中两个byte如何相加

    C#中两个byte如何相加

    可能有的看到这个题目就会觉得这不简单吗?直接用+号相加就行了,可是当你实际操作运行的时候就会发现有错误了,那么是什么错误?那该如何让C#中两个byte相加呢?通过下面这篇文章来一起学习学习吧。
    2016-11-11

最新评论