django,flask下载文件

with open(file_path + file_name, "rb") as f:
    file_cont = f.read()
response = FileResponse(file_cont)
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename="id_rsa.txt"'
return response

django的一种写法,还有好几种,不一一列出

application/octet-stream:内容类型

attachment:是否弹出确认框,加上直接下载

filename="id_rsa.txt" :下载出的文件名称

flask的写法,还有好几种,不一一列出

response = make_response(send_from_directory(file_path, file_name, as_attachment=True))
response.headers["Content-Disposition"] = 
"attachment;filename={}".format(file_path.encode().decode('latin-1'))
return send_from_directory(file_path, file_name, as_attachment=True)


response = Response(file_cont)
response.headers['Content-Type'] = 'application/octet-stream'
response.headers['Content-Disposition'] = 'attachment;filename="id_rsa.txt"'
return response

 

你可能感兴趣的:(python基础,python)