zoukankan      html  css  js  c++  java
  • 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'];无法找到
     
  • 相关阅读:
    HDU-4609 3-idiots FFT
    HDU-1402 A * B Problem Plus FFT(快速傅立叶变化)
    HDU-1007 Quoit Design 平面最近点对
    POJ-3714 Raid 平面最近点对
    HDU-4631 Sad Love Story 平面最近点对
    HDU-4630 No Pain No Game 树状数组+离线操作
    HDU-4628 Pieces 搜索 | DP
    HDU-4627 The Unsolvable Problem 简单数学
    HDU-4638 Group 树状数组+离线
    HDU-4635 Strongly connected 强连通,缩点
  • 原文地址:https://www.cnblogs.com/spaceapp/p/10844084.html
Copyright © 2011-2022 走看看