asp.net mvc文件下载
一、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(); }