[转][html]大文件下载
上面代码来自微软,用于下载大文件。
下面代码来自 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[10000]; // 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 > 0) { // Verify that the client is connected. if (Response.IsClientConnected) { // Read the data in buffer. length = iStream.Read(buffer, 0, 10000); // Write the data to the current output stream. Response.OutputStream.Write(buffer, 0, length); // Flush the data to the HTML output. Response.Flush(); buffer = new Byte[10000]; dataToRead = dataToRead - length; } else { //prevent infinite loop if user disconnects dataToRead = -1; } } } 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 > 0) { List<string> listFJ = new List<string>();//保存附件路径 List<string> listFJName = new List<string>();//保存附件名字 for (int i = 0; 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"), 9);//压缩文件 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 = 0; foreach (string file in filenames) { //打开压缩文件 FileStream fs = File.OpenRead(file);//文件地址 byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, 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, 0, buffer.Length); m++; } } catch { throw; } finally { s.Finish(); s.Close(); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!