flask 提供文件下载

@app.route(‘/download/<filename>’)
def download_file(filename):
defsend_file():
store_path = os.path.join(UPLOAD_FOLDER,filename)
withopen(store_path, ‘rb’) as targetfile:
while1:
data = targetfile.read(1 * 1024 * 500) # 每次读取1MB (可用限速)
ifnot data:
break
yield data
response = Response(send_file(), content_type=’application/octet-stream’)
response.headers[“Content-disposition”] = ‘attachment; filename=%s’ % filename # 如果不加上这行代码,导致下图的问题
return response

Flask WSGI: This is a development server

两种方法:

方法一:

from gevent import pywsgi

if __name__ == ‘__main__’:
server = pywsgi.WSGIServer((‘0.0.0.0’, 5000), app)
server.serve_forever()
方法二:

from wsgiref.simple_server import make_server

if __name__ == ‘__main__’:
server = make_server(”, 64570, app)
server.serve_forever()