上面代码来自微软,用于下载大文件。

下面代码来自 http://www.cnblogs.com/smile-wei/p/4159213.html

System.IO.Stream iStream = null;

// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[]; // Length of the file:
int length; // Total bytes to read:
long dataToRead; // Identify the file to download including its path.
string filepath = "DownloadFileName"; // Identify the file name.
string filename = System.IO.Path.GetFileName(filepath); try
{
// Open the file.
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
System.IO.FileAccess.Read, System.IO.FileShare.Read); // Total bytes to read:
dataToRead = iStream.Length; Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename); // Read the bytes.
while (dataToRead > )
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, , ); // Write the data to the current output stream.
Response.OutputStream.Write(buffer, , length); // Flush the data to the HTML output.
Response.Flush(); buffer = new Byte[];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -;
}
}
}
catch (Exception ex)
{
// Trap the error, if any.
Response.Write("Error : " + ex.Message);
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
}
protected void Btn_down_Click(object sender, EventArgs e)
{
if (ckl_ck.Items.Count > )
{
List<string> listFJ = new List<string>();//保存附件路径
List<string> listFJName = new List<string>();//保存附件名字
for (int i = ; i < ckl_ck.Items.Count; i++)
{
if (ckl_ck.Items[i].Selected)
{
listFJ.Add(Server.MapPath("Resource/Help/") + ckl_ck.Items[i].Text);
listFJName.Add(ckl_ck.Items[i].Text);
} }
string time = DateTime.Now.Ticks.ToString();
ZipFileMain(listFJ.ToArray(), listFJName.ToArray(), Server.MapPath("Resource/Help/" + time + ".zip"), );//压缩文件
DownloadFile(Server.UrlEncode("附件.zip"), Server.MapPath("Resource/Help/" + time + ".zip"));//下载文件
}
}
private void DownloadFile(string fileName, string filePath)
{
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();
File.Delete(filePath);//删除已下载文件
Response.End();
} /// <summary>
/// 压缩文件
/// </summary>
/// <param name="fileName">要压缩的所有文件(完全路径)</param>
/// <param name="fileName">文件名称</param>
/// <param name="name">压缩后文件路径</param>
/// <param name="Level">压缩级别</param>
public void ZipFileMain(string[] filenames, string[] fileName, string name, int Level)
{
ZipOutputStream s = new ZipOutputStream(File.Create(name));
Crc32 crc = new Crc32();
//压缩级别
s.SetLevel(Level); // 0 - store only to 9 - means best compression
try
{
int m = ;
foreach (string file in filenames)
{
//打开压缩文件
FileStream fs = File.OpenRead(file);//文件地址
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, , buffer.Length);
//建立压缩实体
ZipEntry entry = new ZipEntry(fileName[m].ToString());//原文件名
//时间
entry.DateTime = DateTime.Now;
//空间大小
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, , buffer.Length);
m++;
}
}
catch
{
throw;
}
finally
{
s.Finish();
s.Close();
}
}

最新文章

  1. JavaScript——正则表达式
  2. STL---list(列表)
  3. Quick-Cocos2d-x v3.3 异步加载Spine方案 转
  4. shell之脚本练习
  5. 典型的js页面
  6. lintcode :Binary Tree Preorder Traversal 二叉树的前序遍历
  7. [转]C#自定义开关按钮控件--附带第一个私活项目截图
  8. hdu 4915 Parenthese sequence(模拟)2014多培训学校5现场
  9. 这是一名Java学者关于学习方向的建议
  10. iOS MJRefresh下拉、上拉刷新自定义以及系统详细讲解
  11. mybatis 参数格式异常-- Error querying database. Cause: java.lang.NumberFormatException: For input string
  12. HDU - 4734 F(x) (数位dp)
  13. Spring IOC(四)总结升华篇
  14. 2018 宁夏省赛 F. Moving On
  15. Leetcode 870. 优势洗牌
  16. 【HDOJ3567】【预处理bfs+映射+康拓展开hash】
  17. USB之HID类Set_Report Request[调试手记1]
  18. Python导入自定义类时显示错误:attempted relative import beyond top-level package
  19. 【JavaScript】脚本
  20. SHA1加密算法 java

热门文章

  1. BloomFilter&ndash;大规模数据处理利器
  2. 小图标变为字体@font-face
  3. 微信小程序封装storage(含错误处理)
  4. 关闭定时器(setTimeout/clearTimeout|setInterval/clearInterval)
  5. mysql中limit的用法详解[数据分页常用]
  6. Python3 字典Dict(十三)
  7. 用压测模拟并发、并发处理(synchronized,redis分布式锁)
  8. maven 的repository index构建
  9. 二叉树题目集合 python
  10. # 20155327 2016-2017-4 《Java程序设计》第8周学习总结