我试图在ASP.NET MVC中实现以下功能:
到目前为止,这是我最成功的方法:
[HttpPost]
public ActionResult Download(List<string> fileIDs)
{
List<byte[]> blobs = new List<byte[]>();
foreach (var id in fileIDs)
{
//db.GetSelectedBlob returns a byte[] from the database
//that matches the inputparameter 'id'
byte[] blob = db.GetSelectedBlob(id);
blobs.Add(blob);
}
int counter = 0;
MemoryStream mso = new MemoryStream();
using (var zip = new ZipArchive(mso, ZipArchiveMode.Create, true))
{
foreach (var blob in blobs)
{
var file = File(blob, MediaTypeNames.Text.Xml, "UBL" + counter);
ZipArchiveEntry fileInArchive = zip.CreateEntry(file.FileDownloadName, CompressionLevel.Optimal);
counter++;
using (Stream entryStream = fileInArchive.Open())
using (var fileToCompressedStream = new MemoryStream(blob))
{
fileToCompressedStream.CopyTo(entryStream);
}
}
}
return File(mso.GetBuffer(), "application/zip", "UBL.zip");
}
zip功能有效(但不完全合适),下载的文件夹包含以下文件(即与我的数据库查询匹配的正确文件数):
所以,我想要解决的问题如下:
提前感谢您的帮助!
答案 0 :(得分:0)
确保您的文件扩展名为.xml:
var file = File(blob, MediaTypeNames.Text.Xml, "UBL" + counter + ".xml");