c# - Trying to Download File(s) from a List variable into a zip file
问题描述
So as the question states i am trying to download files from a List variable that I am storing filepaths from the database
I am grabbing the paths from the db and storing them in allPaths
using (var db = new Db())
{
// Get Specific years and months
allPaths = db.ClientStatement_Inventory
.Where(x =>
x.accountNum == acctNum &&
years.Contains(x.statementYear) &&
months.Contains(x.statementMonth))
.Select(c => c.statementPath)
.ToList();
}
I am then trying to download the files that are stored in allPaths variable like so: When it is hitting my memorystream i am getting an error:
ReadTimeout = 'memoryStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'
And then when it hits my ZipArchive using statement it is giving me this error:
Entries = 'archive.Entries' threw an exception of type 'System.NotSupportedException'
using (var memoryStream = new MemoryStream())
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
for (int i = 0; i < allPaths.Count; i++)
{
archive.CreateEntryFromFile(allPaths[i], allPaths[i]);
}
}
return File(memoryStream.ToArray(), "application/zip", "Attachments.zip");
}
I honestly have no idea if I am even remotely doing this right but I have been taking pieces from different Stack Overflow questions and Google but cannot get anything to work.
解决方案
如果您只是尝试压缩所有文件并保存到磁盘,则应该这样做。
using (var memoryStream = new MemoryStream())
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
for (int i = 0; i < allPaths.Count; i++)
{
archive.CreateEntryFromFile(allPaths[i], allPaths[i]);
}
}
using (var fileStream = new FileStream(@"All.zip", FileMode.Create))
{
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.CopyTo(fileStream);
}
}
推荐阅读
- php - 如何使用json将数据从一个php文件传递到另一个php文件
- php - 通过 jQuery + Ajax 将带有 Google reCaptcha v2 令牌的图像文件发送到 PHP
- android-studio - 如何让便携式 Intellij IDEA Ultimate 被 Flutter Doctor 识别?
- flutter - 更改可见性后如何防止元素在列中移动?
- sqlite - SQLite:创建相等的日期范围并根据它们查询数据?
- javascript - 如何在功能组件中使用 react-typed 以及 refs?
- javascript - 加载 div 时自动播放音频
- python-3.x - 如何从python中的变量内容打印特定的数值
- php - PayPal webhook:如何识别与 webhook 事件相关的交易
- javascript - 当 SideBar 在 React 上折叠和展开时如何放置间距