您的位置:首页 > 编程语言 > Go语言

django 实现文件下载功能

2017-07-21 12:54 288 查看
以下是一个例子

先上接口:

url(r'^api/download_excel/$', APIs.DownloadScoreExcel),


# download score excel
def DownloadScoreExcel(aRequest):
file_name = aRequest.GET['file_name']
file = os.getcwd() + '/tAPP/tAPPFile/' + file_name # 文件位置
from django.utils.encoding import smart_str
# mimetype   变成了 content_type 在 django 1.7以及更高的办恩重
response = HttpResponse(content_type='application/force-download')  # mimetype is replaced by content_type for django 1.7
response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
response['X-Sendfile'] = smart_str(file)
response.write(open(file,'rb').read())

return response
在flask中下载文件很简单:
# return excel
@app.route("/download/<id>", methods=['GET'])
def down_excel(id):    #id 是文件名
path = os.getcwd() + "/download/" #我的文件存放的路径
if os.path.isfile(path + id):
return send_from_directory(path, id)
else:
return jsonify(status='没有你要的文件', code=404)


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  django web