Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从Azure blob存储下载多个文件作为zip文件_C#_Asp.net Core Webapi_Azure Blob Storage - Fatal编程技术网

C# 从Azure blob存储下载多个文件作为zip文件

C# 从Azure blob存储下载多个文件作为zip文件,c#,asp.net-core-webapi,azure-blob-storage,C#,Asp.net Core Webapi,Azure Blob Storage,我尝试从.NETCore3.1中的WebAPI下载多个文件,并将它们放在响应正文中编写的zip存档中。 当我用浏览器下载zip文件时,zip文件已损坏 控制器: 公共异步任务下载FileAsync([FromServices]IFileService fileService[FromBody]IEnumerable fileId,Guid folderId) { HttpContext.Response.Headers.Add(“内容处置“,$”{DispositionTypeNames.Att

我尝试从.NETCore3.1中的WebAPI下载多个文件,并将它们放在响应正文中编写的zip存档中。 当我用浏览器下载zip文件时,zip文件已损坏

控制器:

公共异步任务下载FileAsync([FromServices]IFileService fileService[FromBody]IEnumerable fileId,Guid folderId)
{
HttpContext.Response.Headers.Add(“内容处置“,$”{DispositionTypeNames.Attachment};文件名*=files.zip”);
HttpContext.Response.ContentType=MediaTypeNames.Application.Zip;
等待fileService.GetZipAsync(User.UserId(),folderId,fileid,HttpContext.Response.Body);
}
服务:

public异步任务GetZipAsync(Guid userId、Guid folderId、IEnumerable fileid、Stream-Stream)
{
使用(ZipArchive zip=new ZipArchive(stream,ZipArchiveMode.Create,leaveOpen:true))
{
foreach(fileid中的Guid id)
{
//元数据存储在sql数据库中,文件是自定义类型
File File=await\u fileRepository.GetFileAsync(folderId,id);
使用Stream entry=zip.CreateEntry(file.Name,CompressionLevel.Optimal).Open();
wait_fileStorage.WriteBlobAsync(userId、folderId、file.Name、entry);
}
}
}
存储库:

公共异步任务WriteBlobAsync(Guid用户ID、Guid文件夹ID、字符串名称、流输出)
{
BlobContainerClient容器=Client.GetBlobContainerClient(userId.ToString());
wait container.GetBlobClient($“{folderId}/{name}”).DownloadToAsync(输出);
}
但下载的zip存档无法使用:


我做错了什么?

方法1。

您可以尝试处置
ZipArchive
,错误应该得到解决

public async Task GetZipAsync(Guid userId, Guid folderId, IEnumerable<Guid> fileIds, Stream stream)
{
    using (ZipArchive zip = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true))
    {
        foreach (Guid id in fileIds)
        {
            // metadata stored in a sql database, File is a custom type
            File file = await _fileRepository.GetFileAsync(folderId, id);
            using Stream entry = zip.CreateEntry(file.Name, CompressionLevel.Optimal).Open();
            await _fileStorage.WriteBlobAsync(userId, folderId, file.Name, entry);
        }
        //I added this line 
        zip.Dispose();
    }
}
public异步任务GetZipAsync(Guid userId、Guid folderId、IEnumerable fileid、Stream-Stream)
{
使用(ZipArchive zip=new ZipArchive(stream,ZipArchiveMode.Create,leaveOpen:true))
{
foreach(fileid中的Guid id)
{
//元数据存储在sql数据库中,文件是自定义类型
File File=await\u fileRepository.GetFileAsync(folderId,id);
使用Stream entry=zip.CreateEntry(file.Name,CompressionLevel.Optimal).Open();
wait_fileStorage.WriteBlobAsync(userId、folderId、file.Name、entry);
}
//我加了这一行
zip.Dispose();
}
}
方法2.

您可以将流的自动刷新属性设置为true

有关更多详细信息,请参阅下面的链接


好吧,我觉得自己在这件事上有点愚蠢

这段代码在后端非常好,我的问题在客户端


我的axios请求配置中缺少一个
{responseType:'arraybuffer'}

嗯<代码>使用自动调用
处置
。两次处理
zip
会有什么帮助?@Zer0对不起,我添加了错误的位置。我已经更新了,试试看。这仍然是一个双重处理。那条线不需要存在。这就是
使用
关键字所做的。@Zer0从另一个线程中选择解决方案不是一个好做法,我已经在整个软件中搜索过了,没有找到任何有效的解决方案。链接的帖子已经过测试,但没有成功。就像说@Zer0
Dispose()
两次一样,ZipArchive没有理由这样做