我正在处理一个MVC项目,在其中创建动态pdf文件(使用wkhtmltopdf),我希望将它们返回到zip文件中。 pdf文件是实时生成的 - 我不需要存储它们,所以我的代码来返回一个单独的文件是:
File(pdfBytes, "application/pdf", "file_name")
浏览一下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);
在我的情况下,我想创建 N 个 pdf 文件,并将其作为 zip 文件返回给视图。就像这样:ZipFile zip = new ZipFile();
foreach(var html in foundRawHTML)
{
//create pdf
//append pdf to zip
}
return zip;
尽管这是不可行的,因为:
- ZipFile和File是静态的,无法实例化
- 没有办法在飞行中(在内存中)将文件添加到zip文件中
欢迎任何帮助