简体   繁体   English

C#IFormFile作为ZipFile

[英]C# IFormFile as ZipFile

I have a REST API endpoint which receives zip file on .Net Core 1.1. 我有一个REST API端点,该端点在.Net Core 1.1上接收zip文件。 I'm getting IFormFile from request like this 我从这样的请求中获取IFormFile

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

And then I need to pass it to service method from .Net Standard 1.5, where IFormFile is not supported. 然后,我需要将其传递给.Net Standard 1.5中不支持IFormFile的服务方法。

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? 所以问题是:如何将IFormFile转换为ZipFile或Standard 1.5支持的其他类型,或者也许有一些更适当的方式来处理zip文件? Thanks! 谢谢!

IFormFile is just a wrapper for the received file. IFormFile只是接收的文件的包装。 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. 或者,您也可以将流复制到文件系统中的物理文件中。

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. 但这基本上取决于您实际要对上传的文件执行的操作,因此您应该从该方向开始,然后将IFormFile转换为所需的文件。


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. 如果要以ZIP格式打开文件并从中提取内容,则可以尝试采用流的ZipArchive构造函数 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM