My scenario is Client side will upload a zipped file.
Then in the back-end asp.net core, the file is retrieve as IFormFile.
How do I extract this IFormFile and pick a specific file inside it?
I'm afraid you may take ZipArchive package into consideration. Pls install this package first to try my code snippet<PackageReference Include="System.IO.Compression" Version="4.3.0" />
Here's my controller method:
[HttpPost]
public async Task<IActionResult> Upload(FileModel file)
{
var stream = file.myfile.OpenReadStream();
var archive = new ZipArchive(stream);
ZipArchiveEntry innerFile = archive.GetEntry("code.txt");
var filePath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\files", "code.txt");
innerFile.ExtractToFile(filePath);
return Ok();
}
And this is my test view:
@model WebAppMvc.Models.FileModel
<form enctype="multipart/form-data" method="post">
<dl>
<dt>
<label asp-for="myfile"></label>
</dt>
<dd>
<input asp-for="myfile" type="file">
<span asp-validation-for="myfile"></span>
</dd>
</dl>
<input asp-page-handler="Upload" class="btn" type="submit" value="Upload" />
</form>
And my model looks like this:
using Microsoft.AspNetCore.Http;
namespace WebAppMvc.Models
{
public class FileModel
{
public IFormFile myfile { set; get; }
}
}