У меня есть следующий код для преобразования имен файлов из сообщения формы в zip-архив и возврата их.
Когда я запускаю его, я получаю файл .zip, но он недействителен. Кто-нибудь видит мою ошибку?
Код: Выделить всё
public IActionResult OnPost()
{
Username = HttpContext.Session.GetString("Username");
string[] FilePaths = Request.Form["FilePath"];
Dictionary fileList = new Dictionary();
byte[] retVal = null;
foreach (var path in FilePaths)
{
string fullPath = Path.Combine(@"C:\Downloads\", Username, path);
byte[] bytes = System.IO.File.ReadAllBytes(fullPath);
fileList.Add(path, bytes);
}
using (MemoryStream zipStream = new MemoryStream())
{
using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Create, true))
{
foreach (var file in fileList)
{
var demoFile = archive.CreateEntry(file.Key);
using (var entryStream = demoFile.Open())
using (var b = new BinaryWriter(entryStream))
{
b.Write(file.Value);
b.Close();
}
}
zipStream.Position = 0;
}
retVal = zipStream.ToArray();
}
return File(retVal, MediaTypeNames.Application.Zip, "test.zip");
}
Источник: https://stackoverflow.com/questions/781 ... valid-file