def download_file(request,department_id,file_name):
    if file_name:
        file = open(os.path.join(MEDIA_ROOT, file_name), 'rb')
        response = FileResponse(file)
        response['Content-Type'] = 'application/octet-stream'
        filename = 'attachment; filename='+file_name
        response['Content-Disposition'] = filename.encode('utf-8', 'ISO-8859-1')  # 设定传输给客户端的文件名称
        return response

Content-disposition 是 MIME 协议的扩展,MIME 协议指示 MIME 用户代理如何显示附加的文件。
Content-disposition其实可以控制用户请求所得的内容存为一个文件的时候提供一个默认的文件名,文件直接在浏览器上显示或者在访问时弹出文件下载对话框。
Content-Disposition就是当用户想把请求所得的内容存为一个文件的时候提供一个默认的文件名。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐