我想用不同的密钥批量下载多个文件(.zip)例如,我有密钥file1 ( abc.txt ),file2 (xyz.pdf)和file3 ( qwe.png ),我想使用它们各自的密钥下载abc.txt和qwe.png,但都以压缩格式一起下载。
我正在尝试使用MVC5控制器C#来做这件事。
这是一个文件。我想在一次去多个文件。
using (client = new AmazonS3Client(AWSCredentials, RegionEndPoint)) {
GetObjectRequest request = new GetObjectRequest {
BucketName = existingBucketName,
Key = newFileName
};
using (GetObjectResponse response = client.GetObject(request)) {
byte[] buffer = ReadFully(response.ResponseStream);
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.AddHeader("content-disposition", "attachment; filename=" + newFileName);
}
}.zip文件是首选输出
发布于 2019-04-02 22:05:34
这很简单,您可以创建一个将文件列表转换为zip文件方法。
public byte[] GetZippedFileFromFileList(List<KeyValuePair<string, byte[]>> fileList)
{
using (MemoryStream zipStream = new MemoryStream())
{
using (ZipArchive zip = new ZipArchive(zipStream, ZipArchiveMode.Create, true))
{
foreach (var file in fileList)
{
var zipEntry = zip.CreateEntry(file.Key);
using (var writer = new StreamWriter(zipEntry.Open()))
{
new MemoryStream(file.Value).WriteTo(writer.BaseStream);
}
}
}
return zipStream.ToArray();
}
}在我的参数fileList中,string是文件名,byte[]是文件。然后在你的控制器中做一些类似这样的事情:
public ActionResult returnZipFile()
{
return this.File(this.GetZippedFileFromFileList(fileList), "application/zip", "myZippedFile.zip"));
}https://stackoverflow.com/questions/55470958
复制相似问题