ASP.NET Web Api 2實(shí)現(xiàn)多文件打包并下載文件的實(shí)例
最近由于工作和個(gè)人事務(wù),站點(diǎn)也好久沒更新了,但這并不影響我對.NET的熱情。站點(diǎn)的更新工作還是得想辦法抽時(shí)間來完成的。
今天利用中午的時(shí)間來寫一篇關(guān)于Asp.Net Web Api下載文件的文章,之前我也寫過類似的文章,請見:《ASP.NET(C#) Web Api通過文件流下載文件的實(shí)例》
本文以這篇文章的基礎(chǔ),提供了ByteArrayContent的下載以及在下載多個(gè)文件時(shí)實(shí)現(xiàn)在服務(wù)器對多文件進(jìn)行壓縮打包后下載的功能。
關(guān)于本文中實(shí)現(xiàn)的在服務(wù)器端用.NET壓縮打包文件功能的過程中,使用到了一個(gè)第方類庫:DotNetZip,具體的使用將在正文中涉及。好了,描述了這么多前言,下面我們進(jìn)入本文示例的正文。
1.首先創(chuàng)建名為:WebApiDownload的Web Api 項(xiàng)目(C#);
2.接著新建一個(gè)空的控制器,命名為:DownloadController;
3.創(chuàng)建一些打包文件和存放臨時(shí)文件的文件夾(downloads),具體請看本文最后提供的示例項(xiàng)目代碼
4.打開NuGet程序包管事器,搜索DotNetZip,如下圖:
搜索到DotNetZip安裝包后,進(jìn)行安裝,以便用于本項(xiàng)目將要實(shí)現(xiàn)多文件打包壓縮的功能,如下圖:
安裝完成DotNetZip包后,我們就可以退出NuGet程序包管理器了,因?yàn)楸卷?xiàng)目為示例項(xiàng)目,不需再添加其他的包。
5.在Models文件夾下創(chuàng)建一個(gè)示例數(shù)據(jù)的類,名為:DemoData,其中的成員和實(shí)現(xiàn)如下:
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; } } } }
6.到這里,我們的準(zhǔn)備工作基本做得差不多了,最后我們只需要在DownloadController控制器中實(shí)現(xiàn)兩個(gè)Action,一個(gè)為:DownloadSingle(提供下載單個(gè)文件的功能),另一個(gè)為:DownloadZip(提供打包壓縮多個(gè)文件并下載的功能)。具體的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"); // 清空臨時(shí)文件夾中的所有臨時(shí)文件 Directory.EnumerateFiles(temp).ToList().ForEach(File.Delete); ClearDownloadDirectory(downloadDir); // 生成新的臨時(shí)文件 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 { } } } } }
到此,本示例的實(shí)現(xiàn)代碼部分就完成了,如果我們此時(shí)打開地址:http://localhost:63161/download/single,瀏覽器會彈出保存文件的提示窗口,如下:
保存此文件后,打開它我們會看到我們的示例數(shù)據(jù)已被保存到本地了,如下:
同樣的,下載壓縮文件你只需要訪問地址:localhost:63161/download/zip 即可,筆者就不再演示了。
最后,附上本示例項(xiàng)目的完整源代碼,點(diǎn)擊這里下載。
以上就是本文的全部內(nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 創(chuàng)建一個(gè)完整的ASP.NET Web API項(xiàng)目
- ASP.NET中Web API的簡單實(shí)例
- ASP.NET MVC Web API HttpClient簡介
- 支持Ajax跨域訪問ASP.NET Web Api 2(Cors)的示例教程
- ASP.NET Web API教程 創(chuàng)建Admin視圖詳細(xì)介紹
- ASP.NET Web API如何將注釋自動生成幫助文檔
- ASP.NET Web API教程 創(chuàng)建Admin控制器實(shí)例分享
- ASP.NET Web API教程 創(chuàng)建域模型的方法詳細(xì)介紹
- .Net Web Api中利用FluentValidate進(jìn)行參數(shù)驗(yàn)證的方法
相關(guān)文章
ASP.NET core Web中使用appsettings.json配置文件的方法
這篇文章主要給大家介紹了在ASP.NET core Web中使用appsettings.json配置文件的方法,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考學(xué)習(xí),下面來一起看看吧。2017-04-04使用Lucene.NET實(shí)現(xiàn)站內(nèi)搜索
提到Lucene,想必大家都有所耳聞,已經(jīng)是數(shù)年前就出現(xiàn)的開源技術(shù)。很多站點(diǎn)都是利用它搭建自己網(wǎng)站的站內(nèi)搜索。由于最近也在做數(shù)據(jù)檢索方面的東西,也學(xué)習(xí)了下Lucene.net的使用。2015-06-06在.NetCore(C#)中使用ODP.NET Core+Dapper操作Oracle數(shù)據(jù)庫
這篇文章主要介紹了在.NetCore(C#)中使用ODP.NET Core+Dapper操作Oracle數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02使用Spring.Net框架實(shí)現(xiàn)多數(shù)據(jù)庫
這篇文章介紹了Spring.Net框架實(shí)現(xiàn)多數(shù)據(jù)庫的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03ASP.NET簡單獲取服務(wù)端和客戶端計(jì)算機(jī)名稱的方法
這篇文章主要介紹了ASP.NET簡單獲取服務(wù)端和客戶端計(jì)算機(jī)名稱的方法,涉及asp.net獲取服務(wù)器端計(jì)算機(jī)名以及根據(jù)IP獲取客戶端主機(jī)名的相關(guān)技巧,需要的朋友可以參考下2016-08-08使用pdfbox實(shí)現(xiàn)pdf文本提取和合并功能示例
這篇文章主要介紹了使用pdfbox實(shí)現(xiàn)pdf文本提取和合并功能示例,大家參考使用吧2014-01-01