关于下载(前后端)第1部分
1.前端点击下载
<a href="/static/images/myimg.jpg" download="picture">下载图片</a>
其中download属性值是下载的文件名
2.后端输出图片
@app.route('/download/') def down(): path = app.root_path+'/static/images/myimg.jpg' file = open(path, 'rb') # caution: mode default 'rt' content = file.read()
file.close() return content,{'Content-Type':'image/jpeg'}
3.后端文件下载
@app.route('/download/') def down(): path = app.root_path+'/static/images/myimg.jpg' file = open(path, 'rb') # caution: mode default 'rt' content = file.read() file.close() size = os.path.getsize(path) filename = os.path.basename(path) return content,{ 'Content-type':'application/octet-stream', 'Accept-Ranges':'bytes', 'Accept-Length':size, 'Content-Disposition':'attachment;filename='+filename}