一、zip打包下載
1.依賴引用:ICSharpCode.SharpZipLib

2.設定網站有單獨文件服務器,網站目錄下有虛擬路徑FileFolder,通過虛擬路徑將文件映射到文件服務器。

設定根據Guid id可以獲取到所需的文件集合
/// <summary> /// 下載zip文件 /// </summary> /// <param name="id"></param> public void DownloadFiles(Guid id) { var files = fileservice.GetFiles(c => c.GroupId == id).ToList(); List<string> paths = new List<string>(files.Count); //數據庫文件存儲示例:\FileFolder\file/20161215170146_9767.pdf string savePath = HttpContext.Server.MapPath(@"~\FileFolder\"); foreach (var file in files) { //驗證文件是否存在... paths.Add(savePath + file.FilePath.Replace(@"\FileFolder\", "")); } string downloadName = "批量下載" + files[0].FileName + "等"; HttpContext.Response.Clear(); HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + downloadName + ".zip"); HttpContext.Response.ContentType = "application/zip"; HttpContext.Response.CacheControl = "Private"; HttpContext.Response.Cache.SetExpires(DateTime.Now.AddMinutes(3)); DownloadZipToBrowser(paths); }
/// <summary> /// 下載壓縮文件 /// </summary> /// <param name="list"></param> private void DownloadZipToBrowser(IEnumerable<string> list) { ZipOutputStream zipOutputStream = null; var response = HttpContext.Response; try { byte[] buffer = new byte[4096]; zipOutputStream = new ZipOutputStream(response.OutputStream); zipOutputStream.SetLevel(6); //0-9, 9 being the highest level of compression foreach (string fileName in list) { string filepath = Server.MapPath(fileName); Stream fs = System.IO.File.OpenRead(filepath); ZipEntry entry = new ZipEntry(Path.GetFileName(filepath)); entry.Size = fs.Length; zipOutputStream.PutNextEntry(entry); int count = fs.Read(buffer, 0, buffer.Length); while (count > 0) { zipOutputStream.Write(buffer, 0, count); count = fs.Read(buffer, 0, buffer.Length); if (!response.IsClientConnected) { break; } response.Flush(); } fs.Close(); } } catch (Exception) { } finally { if (zipOutputStream != null) zipOutputStream.Close(); response.Flush(); response.End(); } }
二、直接下載某一個文件:
/// <summary> /// 下載文件 /// </summary> /// <param name="id">FileId</param> public void DownloadFile(int id ) { string savePath = HttpContext.Server.MapPath(@"~\FileFolder\"); var fileinfo = fileservice.Find(id); string fullname = savePath + fileinfo.FilePath.Replace(@"\FileFolder\", ""); //string fullname = fileinfo.FilePath; //判斷文件是否存在 if (!System.IO.File.Exists(fullname)) { Response.Write("該文件不存在服務器上"); Response.End(); return; } FileInfo fi = new FileInfo(fullname); //**********處理可以解決文件類型問題 string fileextname = fi.Extension; string DEFAULT_CONTENT_TYPE = "application/unknown"; RegistryKey regkey, fileextkey; string filecontenttype; try { regkey = Registry.ClassesRoot; fileextkey = regkey.OpenSubKey(fileextname); filecontenttype = fileextkey.GetValue("Content Type", DEFAULT_CONTENT_TYPE).ToString(); } catch { filecontenttype = DEFAULT_CONTENT_TYPE; } //**********end Response.Clear(); Response.Charset = "utf-8"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileinfo.FileName, System.Text.Encoding.UTF8)); Response.ContentType = filecontenttype; Response.WriteFile(fullname); Response.End(); }
