Asp.net Core Web API-将多个图像从Blob存储下载到zip文件(通过Axios get)

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

zip file error

图像使用天蓝色斑点存储。

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

我的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,bytes.Length);
                    }
                }
            }

            archiveFile = archiveStream.ToArray();
        }

        return archiveFile;
    }
liyinjie86 回答:Asp.net Core Web API-将多个图像从Blob存储下载到zip文件(通过Axios get)

这只是更新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();
            })
        })
    },
本文链接:https://www.f2er.com/3165468.html

大家都在问