以下是一个例子
先上接口:
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/", 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)