vue element中axios下载文件(后端Python)
程序员文章站
2023-12-01 14:01:40
•axios 接受文件流,需要设置 {responsetype:'arraybuffer'}
axios.post(
apiurl,
fo...
•axios 接受文件流,需要设置 {responsetype:'arraybuffer'}
axios.post( apiurl, formdata, {responsetype:'arraybuffer'} ).then(res=> { if (res.status === 200) { let blob = new blob([res.data], { type: res.headers['content-type'] }); const filename = res.headers['content-disposition']; const title = filename && (filename.indexof('filename=') !== -1) ? filename.split('=')[1] : 'download'; require('script-loader!file-saver'); saveas(blob, title); } }) .catch();
注: axios 中 response 表示服务器响应的数据类型,可以是 arraybuffer , blob, document , json , text , stream . 默认为: json
•后端发送文件:python
from flask import send_from_directory @admin_bp.route('/tasksothers/download', methods=["get", "post"]) @auth.login_required def api_tasksothers_download(): root_path = '' src_name = "a.sql" upload_path = os.path.join(root_path, src_name) print("upload_path =", upload_path) if os.path.isfile(upload_path): response = send_from_directory(root_path, src_name, as_attachment=true) print("response: ",response) response.headers["access-control-expose-headers"] = "content-disposition" print("response: ", response.headers) return response
注: 如果 response.header 中没有添加 access-control-expose-headers 这个参数(代表:服务器允许浏览器访问的头(headers)的白名单),vue中就无法获取 content-disposition,即 res.headers['content-disposition'];无法找到
总结
以上所述是小编给大家给大家介绍的vue element中axios下载文件(后端python),希望对大家有所帮助
上一篇: javafx实现图片3D翻转效果方法实例