- 根据公司需求,最近需要做一个文件下载功能,使用flask做后台服务,vue写前端测试。
- flask实现接收文件时先根据指定的人进行创建文件夹
if userid and file:
userid_path = r'D:\bmd\bmd_server\src\company\flask_server\files\{}'.format(userid)
if os.path.exists(userid_path):
tel_file_path = userid_path + '\TEL'
if os.path.exists(tel_file_path):
pass
else:
os.mkdir(tel_file_path)
else:
tel_file_path = userid_path + '\TEL'
os.makedirs(tel_file_path)
tel_file_txt = tel_file_path + r'\{}.txt'.format(userid)
if os.path.exists(tel_file_txt):
os.remove(tel_file_txt)`
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
flask 返回指定位置的文件给前端
@tel.route('/download',methods=["GET","POST"])
def download():
try:
return send_file(r'D:\bmd\bmd_server\src\company\flask_server\files\102888\TEL\',mimetype='text/csv',
attachment_filename="",as_attachment=True)
except Exception as E:
current_app.log.info("下载失败")
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
前端VUE接收到创建下载链接进行下载,
const url = '/tel/download';
let formData = new FormData();
let res = await post(url,formData);
const conent = res.data
// (conent);
const blob = new Blob([conent])
console.log(blob)
const fileName = '空号.txt'
const link = document.createElement('a')
link.download = fileName // a标签添加属性
link.style.display = 'none'
link.href = URL.createObjectURL(blob)
document.body.appendChild(link)
link.click() // 执行下载
URL.revokeObjectURL(link.href) // 释放url
document.body.removeChild(link) // 释
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16