一篇关于Asp.Net Web Api下载文件的文章,之前我也写过类似的文章,请见:《ASP.NET(C#) Web Api通过文件流下载文件到本地实例》
本文以这篇文章的基础,提供了ByteArrayContent的下载以及在下载多个文件时实现在服务器对多文件进行压缩打包后下载的功能。
关于本文中实现的在服务器端用.NET压缩打包文件功能的过程中,使用到了一个第方类库:DotNetZip,具体的使用将在正文中涉及。好了,描述了这么多前言,下面我们进入本文示例的正文。
一、创建项目
1.1 首先创建名为:WebApiDownload的Web Api 项目(C#);
1.2 接着新建一个空的控制器,命名为:DownloadController;
1.3 创建一些打包文件和存放临时文件的文件夹(downloads),具体请看本文最后提供的示例项目代码
1.4 打开NuGet程序包管事器,搜索DotNetZip,如下图:
搜索到DotNetZip安装包后,进行安装,以便用于本项目将要实现多文件打包压缩的功能,如下图:
安装完成DotNetZip包后,我们就可以退出NuGet程序包管理器了,因为本项目为示例项目,不需再添加其他的包。
1.5 在Models文件夹下创建一个示例数据的类,名为:DemoData,其中的成员和实现如下:
using System.Collections.Generic; namespace WebApiDownload.Models { public class DemoData { public static readonly List<List<string>> Contacts = new List<List<string>>(); public static readonly List<string> File1 = new List<string> { "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]" }; public static readonly List<string> File2 = new List<string> { "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]" }; public static readonly List<string> File3 = new List<string> { "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]" }; public static List<List<string>> GetMultiple { get { if (Contacts.Count <= 0) { Contacts.Add(File1); Contacts.Add(File2); Contacts.Add(File3); } return Contacts; } } } }
1.6 到这里,我们的准备工作基本做得差不多了,最后我们只需要在DownloadController控制器中实现两个Action,一个为:DownloadSingle(提供下载单个文件的功能),另一个为:DownloadZip(提供打包压缩多个文件并下载的功能)。具体的DownloadController完整代码如下:
using System.Linq; using System.Net.Http; using System.Text; using System.Web.Http; using Ionic.Zip; using WebApiDownload.Models; using System; using System.IO; using System.Net; using System.Net.Http.Headers; using System.Threading; using System.Web; namespace WebApiDownload.Controllers { [RoutePrefix("download")] public class DownloadController : ApiController { [HttpGet, Route("single")] public HttpResponseMessage DownloadSingle() { var response = new HttpResponseMessage(); //从List集合中获取byte[] var bytes = DemoData.File1.Select(x => x + "\n").SelectMany(x => Encoding.UTF8.GetBytes(x)).ToArray(); try { var fileName = string.Format("download_single_{0}.txt", DateTime.Now.ToString("yyyyMMddHHmmss")); var content = new ByteArrayContent(bytes); response.Content = content; response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = fileName }; response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); } catch (Exception ex) { response.StatusCode = HttpStatusCode.InternalServerError; response.Content = new StringContent(ex.ToString()); } return response; } [HttpGet, Route("zip")] public HttpResponseMessage DownloadZip() { var response = new HttpResponseMessage(); try { var zipFileName = string.Format("download_compressed_{0}.zip", DateTime.Now.ToString("yyyyMMddHHmmss")); var downloadDir = HttpContext.Current.Server.MapPath($"~/downloads/download"); var archive = $"{downloadDir}/{zipFileName}"; var temp = HttpContext.Current.Server.MapPath("~/downloads/temp"); // 清空临时文件夹中的所有临时文件 Directory.EnumerateFiles(temp).ToList().ForEach(File.Delete); ClearDownloadDirectory(downloadDir); // 生成新的临时文件 var counter = 1; foreach (var c in DemoData.GetMultiple) { var fileName = string.Format("each_file_{0}_{1}.txt", counter, DateTime.Now.ToString("yyyyMMddHHmmss")); if (c.Count <= 0) { continue; } var docPath = string.Format("{0}/{1}", temp, fileName); File.WriteAllLines(docPath, c, Encoding.UTF8); counter++; } Thread.Sleep(500); using (var zip = new ZipFile()) { // Make zip file zip.AddDirectory(temp); zip.Save(archive); } response.Content = new StreamContent(new FileStream(archive, FileMode.Open, FileAccess.Read)); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = zipFileName }; response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); } catch (Exception ex) { response.StatusCode = HttpStatusCode.InternalServerError; response.Content = new StringContent(ex.ToString()); } return response; } private void ClearDownloadDirectory(string directory) { var files = Directory.GetFiles(directory); foreach (var file in files) { try { File.Delete(file); } catch { } } } } }
二、运行示例
2.1 到此,本示例的实现代码部分就完成了,如果我们此时打开地址:http://localhost:63161/download/single,浏览器会弹出保存文件的提示窗口,如下:
2.2 保存此文件后,打开它我们会看到我们的示例数据已被保存到本地了,如下:
文章:Asp.Net Web Api 2利用ByteArrayContent和StreamContent分别实现下载文件示例源码(含多文件压缩功能)