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

Leave a Reply

邮箱地址不会被公开。 必填项已用*标注

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>