我是靠谱客的博主 悦耳煎饼,这篇文章主要介绍.NetCore下多个文件流生成压缩文件,现在分享给大家,希望可以做个参考。

原有系统的附件都是保存在服务器上的,占用了大量的磁盘空间,如今要切换到AWS S3上做文件存储管理。文件打包下载之前的做法是,后台将文件服务器上的多个文件打包到一个压缩文件中,存放在服务器,后端接口返回压缩文件地址供前端下载;为此,特写下这篇文章来记录此处是如何根据文件流来制作压缩包的。

AWS S3更多存取文件的demo需看官方源码,本文中有下载的实例。

需要引用:AWSSDK.Core、AWSSDK.S3,可从NuGet安装;
需要引用:System.IO.Compression,可从NuGet安装;

在ConfigureServices下单例注入AWS类

 

复制代码
1
2
3
4
5
6
7
public void ConfigureServices(IServiceCollection services) { ....... services.AddSingleton<Aws>(); ......... }

Configure下获取AWS实例

 

复制代码
1
2
3
4
5
6
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { //......// Aws.Instance = app.ApplicationServices.GetService<Aws>(); }

AWS类

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
public class Aws { #region Init private readonly IConfiguration _cfg; /// <summary> /// 存储桶名称 /// </summary> private string bucketName; /// <summary> /// aws Id /// </summary> private readonly string awsAccessKeyId; /// <summary> /// aws Id key /// </summary> private readonly string awsSecretAccessKey; private IAmazonS3 client; private AmazonS3Config config = new AmazonS3Config() { ServiceURL = "http://s3.amazonaws.com", RegionEndpoint = Amazon.RegionEndpoint.CNNorth1 //账号在哪个区域就用哪个区域,不然会出现账号401的问题 }; #endregion public Aws(IConfiguration cfg) { _cfg = cfg; awsAccessKeyId = _cfg["AccessKeyId"]; awsSecretAccessKey = _cfg["SecretAccessKey"]; bucketName = _cfg["BucketName"]; client = new AmazonS3Client(awsAccessKeyId, awsSecretAccessKey, config); } /// <summary> /// AWS文件批量下载 /// </summary> /// <param name="fileName">文件相对地址(aws key)</param> /// <returns></returns> public async Task<byte[]> BatchDownload(List<string> fileNames) { try { byte[] res; using (MemoryStream ms = new MemoryStream()) { using (ZipArchive zip = new ZipArchive(ms, ZipArchiveMode.Create, true)) { foreach (var fileName in fileNames) { //通过aws下载文件流 var response = await client.GetObjectAsync(bucketName, newfileName); var pdfStream= response.ResponseStream;//得到文件流 var pdfBytes = StringHelper.StreamToBytes(pdfStream);//文件流转换成字节 ZipArchiveEntry entry = zip.CreateEntry("abc.pdf");//压缩文件内创建一个文件名为“abc.pdf”,流是什么文件格式就用什么格式,此处我的是pdf格式 using (Stream sw = entry.Open()) { sw.Write(pdfBytes, 0, pdfBytes.Length);//将文件的字节写到abc.pdf中 } } InvokeWriteFile(zip);//重新计算压缩文件的大小,此处参考别人的 int nowPos = (int)ms.Position; res = new byte[ms.Length]; ms.Position = 0; ms.Read(res, 0, res.Length); ms.Position = nowPos; } return res; } } catch (Exception ex) { throw ex; } } public void InvokeWriteFile(ZipArchive zipArchive) { foreach (MethodInfo method in zipArchive.GetType().GetRuntimeMethods()) { if (method.Name == "WriteFile") { method.Invoke(zipArchive, new object[0]); } } } }

控制器下载压缩文件

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/// <summary> /// 批量下载 /// </summary> /// <param name="ids"></param> /// <returns></returns> public async Task<IActionResult> Packing(List<int> ids) { var paths = new List<string>(); //根据Id查询本地对应的文件名称,Id换取文件名称 var bytes = await Aws.Instance.BatchDownload(paths).ToList()); Response.ContentType = "application/octet-stream"; var contentDisposition = "attachment;" + "filename=" + HttpUtility.UrlEncode("XXXX.zip"); Response.Headers.Add("Content-Disposition", new string[] { contentDisposition }); Response.ContentLength = bytes.Length; Response.Body.Write(bytes, 0, bytes.Length);//发送读取的内容数据到客户端浏览器 Response.Body.Flush(); return new EmptyResult(); }

前端Ajax调用

 

复制代码
1
2
3
4
5
6
7
8
9
10
//后端返回的是文件流 var ids = [1,2,3]; var url = "/PackingByAws"; var form = $("<form></form>").attr("action", url).attr("method", "post"); 后端为List<Int>参数 ids.forEach(function (value) { form.append($("<input></input>").attr("type", "hidden").attr("name", "ids").attr("value", value)); }) form.appendTo('body').submit().remove();

 

最后

以上就是悦耳煎饼最近收集整理的关于.NetCore下多个文件流生成压缩文件的全部内容,更多相关内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(69)

评论列表共有 0 条评论

立即
投稿
返回
顶部