【问题标题】:Asp.net Core Web API - Downloading multiple images from blob storage to zip file (via Axios get)Asp.net Core Web API - 将多个图像从 blob 存储下载到 zip 文件(通过 Axios 获取)
【发布时间】:2020-02-29 23:18:02
【问题描述】:

我正在尝试下载一个包含许多文件(各种类型)的 zip 文件 - 虽然我确实下载了一个 zip 文件,但不幸的是,当我尝试打开它时会引发以下错误:

zip file error

图像使用 azure blob 存储进行存储。

如果您有任何想法,请提供帮助。

我的 Vue.js 前端有一个按钮,它调用以下内容:

    GetAllAssetResources ({commit},id)
    {
        console.log("Getting Asset Resources:" + id);
        return new Promise((resolve) => {
            axios.get(CONFIG.platformEndPoints+  '/asset/downloadResources/' + id)
            .then( (response) => {
                const url = window.URL.createObjectURL(new Blob([response.data]));
                const link = document.createElement('a');
                link.href = url;
                link.setAttribute('download', 'resources.zip');
                document.body.appendChild(link);
                link.click();
            })
        })
    }

这会调用我的控制器方法(使用 C#):

    [HttpGet]
    public async Task<IActionResult> DownloadResources(string guid)
    {
        return File(await _ResourceService.DownloadAllAssetResources(guid), MediaTypeNames.Application.Octet, "resources");
    }

这会调用我的服务:

    public async Task<byte[]> DownloadAllAssetResources(string guid)
    {
        var assetDetails = await _AssetDetails.FindAsync(guid);
        var resources = assetDetails.res.ToList();
        byte[] archiveFile;

        await using (var archiveStream = new MemoryStream())
        {
            using (var archive = new ZipArchive(archiveStream, ZipArchiveMode.Create, true))
            {
                foreach (var file in resources)
                {
                    var fileName = guid+ "_" + file.name;
                    var zipArchiveEntry = archive.CreateEntry(fileName, CompressionLevel.Fastest);
                    var bytes = await _Storage.GetBytes("resources", fileName);

                    await using (var zipStream = zipArchiveEntry.Open())
                    {
                        zipStream.Write(bytes, 0, bytes.Length);
                    }
                }
            }

            archiveFile = archiveStream.ToArray();
        }

        return archiveFile;
    }

【问题讨论】:

    标签: c# vue.js asp.net-core asp.net-web-api axios


    【解决方案1】:

    这只是更新我的 axios 调用以包含响应类型的情况:

            GetAllAssetResources ({commit},assetDetailid)
        {
            console.log("Getting Asset Resources:" + id);
            return new Promise((resolve) => {
                axios({
                    url: CONFIG.platformEndPoints+  '/asset/downloadResources/' + id,
                    method: 'GET',
                    responseType: 'blob'})
                .then( (response) => {
                    const url = window.URL.createObjectURL(new Blob([response.data]));
                    const link = document.createElement('a');
                    link.href = url;
                    link.setAttribute('download', 'resources.zip');
                    document.body.appendChild(link);
                    link.click();
                })
            })
        },
    

    【讨论】:

      猜你喜欢
      • 2021-08-11
      • 2019-03-09
      • 2020-02-14
      • 2019-03-01
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      • 2018-01-09
      • 2015-10-25
      相关资源
      最近更新 更多