django 下载文件(解决中文路径问题)

def file_download(request):
    """ 下载文件 """
    # 我这里是访问网络地址下载文件
    # 下载本地文件的话直接打开文件下载就ok
    # with open('xxx', mode='rb') as f:
    # 	data = f.read()
    
    res = requests.get(xxx)
    # 文件分块处理(适用于大文件)
    data = res.iter_content()

    # 设置content_type='application/octet-stream' 用于提示下载框
    response = HttpResponse(data, content_type='application/octet-stream')

    # 设置响应头, 文件 & 文件名转义
    response['Content-Disposition'] = 'attachment; filename={}'.format(escape_uri_path(file_object.name))
    return response

猜你喜欢

转载自blog.csdn.net/qq_29339467/article/details/107581572
今日推荐