Asp.Net Web Api 2 实现多文件打包并下载文件示例源码
前言
最近由于工作和個人事務,站點也好久沒更新了,但這并不影響我對.NET的熱情。站點的更新工作還是得想辦法抽時間來完成的。
提要
今天利用中午的時間來寫一篇關于Asp.Net Web Api下載文件的文章,之前我也寫過類似的文章,請見:《ASP.NET(C#) Web Api通過文件流下載文件到本地實例》
本文以這篇文章的基礎,提供了ByteArrayContent的下載以及在下載多個文件時實現在服務器對多文件進行壓縮打包后下載的功能。
關于本文中實現的在服務器端用.NET壓縮打包文件功能的過程中,使用到了一個第方類庫:DotNetZip,具體的使用將在正文中涉及。好了,描述了這么多前言,下面我們進入本文示例的正文。
一、創(chuàng)建項目
1.1 首先創(chuàng)建名為:WebApiDownload的Web Api 項目(C#);
1.2 接著新建一個空的控制器,命名為:DownloadController;
1.3 創(chuàng)建一些打包文件和存放臨時文件的文件夾(downloads),具體請看本文最后提供的示例項目代碼
1.4 打開NuGet程序包管事器,搜索DotNetZip,如下圖:
搜索到DotNetZip安裝包后,進行安裝,以便用于本項目將要實現多文件打包壓縮的功能,如下圖:
安裝完成DotNetZip包后,我們就可以退出NuGet程序包管理器了,因為本項目為示例項目,不需再添加其他的包。
1.5 在Models文件夾下創(chuàng)建一個示例數據的類,名為: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>{"f_1_test_1@example.com","f_1_test_2@example.com","f_1_test_3@example.com","f_1_test_4@example.com","f_1_test_5@example.com"};public static readonly List<string> File2 = new List<string>{"f_2_test_1@example.com","f_2_test_2@example.com","f_2_test_3@example.com","f_2_test_4@example.com","f_2_test_5@example.com"};public static readonly List<string> File3 = new List<string>{"f_3_test_1@example.com","f_3_test_2@example.com","f_3_test_3@example.com","f_3_test_4@example.com","f_3_test_5@example.com"};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 filezip.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分別實現下載文件示例源碼(含多文件壓縮功能)
總結
以上是生活随笔為你收集整理的Asp.Net Web Api 2 实现多文件打包并下载文件示例源码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Pacemaker+Corosync P
- 下一篇: 万能的Entry,两个变量的Model/