C#四种下载文件方法
来源:互联网 发布:免费即时聊天软件 编辑:程序博客网 时间:2024/05/17 08:04
Button5事件在我本机测试有问题。大家可以再看看。
//TransmitFile下载
protected void Button1_Click(object sender, EventArgs e)
{
//Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=1.txt");
string filename = Server.MapPath("/UpLoadFile/1.txt");
Response.TransmitFile(filename);
}
//WriteFile下载
protected void Button2_Click(object sender, EventArgs e)
{
string fileName = "1.txt";//客户端保存的文件名
string filePath = Server.MapPath("/UpLoadFile/1.txt");//路径
FileInfo fileInfo = new FileInfo(filePath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); //加上这行可下载txt文档(通知浏览器是下载文件而不是打开)
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("/UpLoadFile/1.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("/UpLoadFile/1.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();
}
//保存文件并打开
protected void Button5_Click(object sender, EventArgs e)
{
//string filePath = Server.MapPath("/UpLoadFile/");//路径
////这里输入你要下载的url地址
//string remoteUri = filePath;
////输入下载的文件名称
//string fileName = "pic.jpg", myStringWebResource = null;
////创建WebClient实例
//WebClient myWebClient = new WebClient();
//// web地址全名
//myStringWebResource = remoteUri + fileName;
//Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
//// 下载保存文件到程序运行目录下
//myWebClient.DownloadFile(myStringWebResource, fileName);
//Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
//Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath);
//// 打开文件
//Process myProcess = new Process();
//myProcess.StartInfo.FileName = Application.StartupPath + "\\" + fileName;
//myProcess.StartInfo.Verb = "Open";
//myProcess.StartInfo.CreateNoWindow = true;
//myProcess.Start();
}
- C#文件下载四种方法
- C#四种下载文件方法
- C# 文件下载四方法
- C# 文件下载四方法
- C# 文件下载四方法
- C# 文件下载四方法
- C# 文件下载四方法
- C#文件下载四方法
- C# 文件下载四方法
- C# 文件下载四方法
- C# 文件下载四方法
- C# 文件下载四方法
- C#文件下载四方法
- C# 文件下载四方法
- C# 文件下载四方法
- C# 文件下载四方法
- C# 文件下载四方法
- C# 文件下载四方法
- Android camera系统开发之总括
- 一位软件工程师的7年总结 [转]
- SaaS、云计算--搅动未来软件发展
- 云计算时代下网络安全的机遇挑战与未来趋势
- Android camera系统开发之IPC (二)
- C#四种下载文件方法
- Android Service被关闭后自动重启,解决被异常kill 服务
- .Net企业级应用架构设计之表现层设计
- CMAKE的使用
- 特殊字的读音
- ecshop后台批量上传图片方法
- Mybatis 学习笔记 1(非原创 自己整理)
- Android camera系统开发之IPC (三)
- android小记之FTP文件上传