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

Django中ZipFile经HttpResponse返回遇UnicodeDecodeError等问题求助

解决Zip文件下载时的UnicodeDecodeError与文件损坏问题

咱们先拆解你遇到的两个问题的核心根源:

问题1:UnicodeDecodeError的原因

你用open('csvOutput{}.zip'.format(timestamp))打开文件时,默认是文本模式,Python会自动尝试用UTF-8解码文件内容。但Zip是二进制格式,里面的字节根本不是UTF-8编码的文本,直接触发了解码失败的错误。

问题2:Zip文件损坏的原因

即使你改了content_type,但还是用文本模式读取文件,会导致二进制内容被篡改(比如自动转换换行符、截断非文本字节),最终返回的Zip文件结构完全损坏,UnZip自然识别不了。

完整修复方案

下面是修正后的代码,同时做了几个实用的最佳实践优化:

1. 优化Zip文件创建(避免手动关闭文件)

用with语句自动管理ZipFile资源,防止忘记关闭导致的文件不完整:

import zipfile
from django.http import HttpResponse, Http404, FileResponse
import time

# 假设你的timestamp是这样生成的(根据实际业务调整)
timestamp = int(time.time())

# 创建Zip文件:用with自动关闭,无需手动调用close()
with zipfile.ZipFile(f"csvOutput{timestamp}.zip", 'w') as zipped_file:
    zipped_file.write(sms_file_path)
    zipped_file.write(mail_maga_file_path)

2. 正确返回Zip文件响应

核心修复点:

  • 用rb模式打开文件(二进制只读)
  • 修正Content-Disposition的引号错误(你原来的代码末尾多了一个双引号)
  • 可选:用FileResponse替代HttpResponse(Django 2.0+支持,更适合大文件流式传输)

写法一:用HttpResponse(兼容旧版本Django)

try:
    # 关键:以二进制模式打开文件
    with open(f'csvOutput{timestamp}.zip', 'rb') as response_file:
        response = HttpResponse(response_file.read(), content_type="application/zip")
        # 修正引号格式,用f-string更清晰
        response['Content-Disposition'] = f'attachment; filename="csvOutput{timestamp}.zip"'
        return response
except FileNotFoundError:
    raise Http404

写法二:用FileResponse(推荐,更高效)

try:
    # FileResponse会自动处理二进制流,适合大文件,减少内存占用
    response = FileResponse(open(f'csvOutput{timestamp}.zip', 'rb'), content_type="application/zip")
    response['Content-Disposition'] = f'attachment; filename="csvOutput{timestamp}.zip"'
    return response
except FileNotFoundError:
    raise Http404

额外注意事项

  • 如果你生成的Zip文件很大,response_file.read()会把整个文件加载到内存,这时候用FileResponse更优,它会流式传输文件,降低服务器内存压力。
  • 你的原始代码里return response后面的raise Http404永远不会执行,可以直接去掉或者移到异常处理块中。
  • 建议始终用with语句处理文件操作,避免资源泄漏。

内容的提问来源于stack exchange,提问作者Daniel Thompson

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:18:02