C#IFormFile作为ZipFile

编程入门 行业动态 更新时间:2024-10-28 04:19:36
本文介绍了C#IFormFile作为ZipFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个REST API端点,该端点在.Net Core 1.1上接收zip文件。我从这样的请求中获取IFormFile

I have a REST API endpoint which receives zip file on .Net Core 1.1. I'm getting IFormFile from request like this

var zipFile = HttpContext.Request.Form.Files.FirstOrDefault();

然后我需要将其传递给.Net Standard 1.5中的服务方法,其中不支持IFormFile 。

And then I need to pass it to service method from .Net Standard 1.5, where IFormFile is not supported.

所以问题是:如何将IFormFile转换为ZipFile或Standard 1.5支持的其他类型,或者有一些更合适的操作方式压缩文件? 谢谢!

So the question is: how can I convert IFormFile to ZipFile or to some other type which is supported in Standard 1.5, or maybe there is some more proper way to operate with zip files? Thanks!

推荐答案

IFormFile 只是一个包装收到的文件。您仍然应该阅读实际文件。例如,您可以将文件流读取为字节数组,并将其传递给服务:

IFormFile is just a wrapper for the received file. You should still read the actual file do something about it. For example, you could read the file stream into a byte array and pass that to the service:

byte[] fileData; using (var stream = new MemoryStream((int)file.Length)) { file.CopyTo(stream); fileData = stream.ToArray(); }

或者您也可以将流复制到文件系统中的物理文件中。

Or you could copy the stream into a physical file in the file system instead.

但这基本上取决于您实际要对上传的文件进行什么操作,因此您应该从该方向开始并转换 IFormFile 放入所需的内容。

But it basically depends on what you actually want to do with the uploaded file, so you should start from that direction and the convert the IFormFile into the thing you need.

如果要以ZIP格式打开文件并解压缩您可以尝试使用 ZipArchive 构造函数,需要一个流。像这样的东西:

If you want to open the file as a ZIP and extract something from it, you could try the ZipArchive constructor that takes a stream. Something like this:

using (var stream = file.OpenReadStream()) using (var archive = new ZipArchive(stream)) { var innerFile = archive.GetEntry("foo.txt"); // do something with the inner file }

更多推荐

C#IFormFile作为ZipFile

本文发布于:2023-11-14 16:27:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587953.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:IFormFile   ZipFile

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!