Geo*_*rge 2 .net zip asp.net-core
我正在一个MVC项目中,我在其中动态创建要以zip文件返回的pdf文件(wkhtmltopdf)。pdf文件是即时生成的-我不需要存储它们,因此我返回单个文件的代码是:
File(pdfBytes, "application/pdf", "file_name")
Run Code Online (Sandbox Code Playgroud)
看看Microsoft文档,他们的示例遍历了存储的文件:
string startPath = @"c:\example\start";
string zipPath = @"c:\example\result.zip";
string extractPath = @"c:\example\extract";
ZipFile.CreateFromDirectory(startPath, zipPath);
ZipFile.ExtractToDirectory(zipPath, extractPath);
Run Code Online (Sandbox Code Playgroud)
在我的情况下,我想创建N个pdf文件,并将其作为zip文件返回到视图。
ZipFile zip = new ZipFile();
foreach(var html in foundRawHTML)
{
//create pdf
//append pdf to zip
}
return zip;
Run Code Online (Sandbox Code Playgroud)
尽管这是不可行的,因为:
欢迎任何帮助
小智 5
您可以在内存字节数组和System.IO.Compression中的ZipArchive中使用,无需映射本地驱动器:
public static byte[] GetZipArchive(List<InMemoryFile> files)
{
byte[] archiveFile;
using (var archiveStream = new MemoryStream())
{
using (var archive = new ZipArchive(archiveStream, ZipArchiveMode.Create, true))
{
foreach (var file in files)
{
var zipArchiveEntry = archive.CreateEntry(file.FileName, CompressionLevel.Fastest);
using (var zipStream = zipArchiveEntry.Open())
zipStream.Write(file.Content, 0, file.Content.Length);
}
}
archiveFile = archiveStream.ToArray();
}
return archiveFile;
}
public class InMemoryFile
{
public string FileName { get; set; }
public byte[] Content { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1570 次 |
| 最近记录: |