• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

vue element中axios下载文件(后端Python)

vue 搞代码 4年前 (2022-01-08) 7次浏览 已收录 0个评论

这篇文章主要介绍了vue element中axios下载文件(后端Python)的实例代码,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下

•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),希望对大家有所帮助,如果大家有任何本文来源[email protected]搞@^&代*@码)网5疑问欢迎给我留言,小编会及时回复大家的!

以上就是vue element中axios下载文件(后端Python)的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:vue element中axios下载文件(后端Python)

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址