日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > asp.net >内容正文

asp.net

Asp.Net Web Api 2 实现多文件打包并下载文件示例源码

發(fā)布時(shí)間:2025/4/5 asp.net 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Asp.Net Web Api 2 实现多文件打包并下载文件示例源码 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言

最近由于工作和個(gè)人事務(wù),站點(diǎn)也好久沒更新了,但這并不影響我對(duì).NET的熱情。站點(diǎn)的更新工作還是得想辦法抽時(shí)間來完成的。

提要

今天利用中午的時(shí)間來寫一篇關(guān)于Asp.Net Web Api下載文件的文章,之前我也寫過類似的文章,請(qǐng)見:《ASP.NET(C#) Web Api通過文件流下載文件到本地實(shí)例》
本文以這篇文章的基礎(chǔ),提供了ByteArrayContent的下載以及在下載多個(gè)文件時(shí)實(shí)現(xiàn)在服務(wù)器對(duì)多文件進(jìn)行壓縮打包后下載的功能。
關(guān)于本文中實(shí)現(xiàn)的在服務(wù)器端用.NET壓縮打包文件功能的過程中,使用到了一個(gè)第方類庫(kù):DotNetZip,具體的使用將在正文中涉及。好了,描述了這么多前言,下面我們進(jìn)入本文示例的正文。

一、創(chuàng)建項(xiàng)目

1.1 首先創(chuàng)建名為:WebApiDownload的Web Api 項(xiàng)目(C#);

1.2 接著新建一個(gè)空的控制器,命名為:DownloadController;

1.3 創(chuàng)建一些打包文件和存放臨時(shí)文件的文件夾(downloads),具體請(qǐng)看本文最后提供的示例項(xiàng)目代碼

1.4 打開NuGet程序包管事器,搜索DotNetZip,如下圖:


搜索到DotNetZip安裝包后,進(jìn)行安裝,以便用于本項(xiàng)目將要實(shí)現(xiàn)多文件打包壓縮的功能,如下圖:

安裝完成DotNetZip包后,我們就可以退出NuGet程序包管理器了,因?yàn)楸卷?xiàng)目為示例項(xiàng)目,不需再添加其他的包。

1.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;}}} }

1.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 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{}}}} }

二、運(yùn)行示例

2.1 到此,本示例的實(shí)現(xiàn)代碼部分就完成了,如果我們此時(shí)打開地址:http://localhost:63161/download/single,瀏覽器會(huì)彈出保存文件的提示窗口,如下:

2.2 保存此文件后,打開它我們會(huì)看到我們的示例數(shù)據(jù)已被保存到本地了,如下:

我的網(wǎng)站文章:Asp.Net Web Api 2利用ByteArrayContent和StreamContent分別實(shí)現(xiàn)下載文件示例源碼(含多文件壓縮功能)

總結(jié)

以上是生活随笔為你收集整理的Asp.Net Web Api 2 实现多文件打包并下载文件示例源码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。