最近遇到一个需求,就是要生成csv或者是excel,然后提供下载,当时不是提前就备好了下载列表,而是需要根据需求来生成,之前也写过一个类似的php的笔记:https://sulao.cn/post/399.html
好了,我们还是来看看flask的下载文件是如何实现的
1.通过send_from_directory方法返回真实的文件
from flask import Flask,request,send_from_directory import csv import json @tools.route('/generatelist', methods=['POST','GET']) def generateList(): if request.method == 'POST': url = url_for('advtools.List') domain = request.url.replace(request.path, '') post_url = domain+url response = requests.post(post_url) _list = response.json() try: with open('attchment/list.csv', 'w', newline='') as csvfile: cvwrite = csv.writer(csvfile) cvwrite.writerow(['Id','名字','是否启用','drawer','类型','RTSP']) for c in _list['cameras']: cvwrite.writerow((c['id'],c['name'],c['enabled'],c['type'],c['url'])) return "1" except: return "0" curdir = os.path.join(os.getcwd(), 'attchment') response = send_from_directory(curdir, filename='list.csv', as_attachment=True) return response
前端js代码
layui.use(['layer', 'jquery', 'code'], function(){ var layer = layui.layer ,$ = layui.jquery; $('#generate').on('click', function(){ $.post('{{ url_for("advtools.generateList") }}', function(d){ if(d == 1){ layer.msg('生成抓拍机列表成功!', {icon: 1, time: 2000}); var generate_txt = "<a class='layui-btn layui-btn-xs' href=\"{{ url_for('advtools.generateList') }}\" tartget='_blank'><i class='layui-icon layui-icon-download-circle'></i> 下载抓拍机列表文件</a>"; $('.layui-text').append(generate_txt); }else{ layer.alert("抓拍机列表生成失败,请稍后重试!"); } }); }); });
2.使用make_response返回数据流,我重新写了个例子
#!/usr/bin/python3 #coding:utf-8 from flask import Flask,url_for,send_file,send_from_directory,make_response import os app = Flask(__name__) @app.route('/') def index(): return "<a href='%s'>下载文件</a>" % url_for('test') @app.route('/test') def test(): with open('sur.log', 'r', encoding='utf-8') as f: content = f.read() response = make_response(content) response.headers['Content-Disposition'] = 'attchment; filename=test.log' return response if __name__ == "__main__": app.run(debug=True)
使用make_response可以添加headers信息,重命名下载文件名等
还有发送静态文件(static下的文件)的方法,这里不再讲解,因为用的较少