You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用API未触发Zip文件下载,返回乱码文本问题求助

问题排查与解决方案

核心原因

前端通过fetch请求二进制文件时,默认会将响应内容解析为文本格式,导致Zip文件的二进制数据被当成乱码显示;同时fetch不会自动触发浏览器的下载行为,需要手动处理Blob并生成下载链接。

后端代码优化(可选但推荐)

  1. 将Content-Type从application/octet-stream改为更精确的application/zip,让浏览器更明确识别文件类型:
[HttpGet("downloadfile")]
public async Task<IActionResult> Downloadfile(string userId, CancellationToken ct)
{
    string docId = $"tinyurl:{userId}";
    DocumentResponse response1 = await blobManager.GetCustomUrlByUserId(docId, ct);
    TinyUrl tinyUrl = JsonConvert.DeserializeObject<TinyUrl>(response1.Content);

    var response = await wc.GetAsync(tinyUrl.ActualUrl, HttpCompletionOption.ResponseHeadersRead, ct);

    if (!response.IsSuccessStatusCode)
    {
        return NotFound();
    }
    var contentStream = await response.Content.ReadAsStreamAsync();
    var fileName = "file.zip";
    // 修改Content-Type为application/zip
    return new FileStreamResult(contentStream, "application/zip")
    {
        FileDownloadName = fileName
    };
}
  1. 当前代码使用HttpCompletionOption.ResponseHeadersRead的方式是正确的,能高效获取流且避免提前关闭,无需调整。

前端代码修复(必须)

修改DownloadZip函数,处理响应为Blob并手动触发下载:

export const DownloadZip = async (uri) => {
  const method = 'GET';
  const headers = await auth.getTokenHeader(true);
  headers.append('Accept', 'application/zip'); // 与后端Content-Type对应

  const response = await retryFetch(uri, { method, headers });
  
  if (!response.ok) {
    throw new Error('下载请求失败');
  }

  // 将响应转为Blob对象
  const blob = await response.blob();
  // 创建临时下载链接
  const url = window.URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.href = url;
  a.download = 'file.zip'; // 指定下载文件名
  document.body.appendChild(a);
  a.click();
  // 清理临时资源
  window.URL.revokeObjectURL(url);
  document.body.removeChild(a);
};

额外优化(可选)

如果后端返回的文件名是动态的,可以从响应头Content-Disposition中提取,替换前端硬编码的文件名:

const contentDisposition = response.headers.get('Content-Disposition');
const fileName = contentDisposition?.split('filename=')[1] || 'file.zip';
a.download = fileName;

内容的提问来源于stack exchange,提问作者Jug Patel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 09:57:19