后端返回文件流格式,前端vue 导出下载表格

//日期时间格式化

export const formatDate = (edate, type) => {

  var date = new Date(edate);

  var year = date.getFullYear(); //年

  var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; //月

  var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); //日

  var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); //时

  var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); //分

  var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); //秒

  var milliseconds = date.getMilliseconds() < 10 ? "0" + date.getMilliseconds() : date.getMilliseconds(); //毫秒

  if (type == 1) {

      return year + "-" + month + "-" + day + "   " + hour + ":" + minutes + ":" + seconds + "." + milliseconds;

  } else if (type == 2) {

      return year + "-" + month + "-" + day + "   " + hour + ":" + minutes + ":" + seconds;

  } else if (type == 3) {

      return year + "-" + month + "-" + day;

  } else if (type == 4) {

      return year + '' + month;

  }

};

// 根据后端返回的blob数据进行下载导出

export function getDownload(res,name) {

    const blob = new Blob([res]);

    // const blob = new Blob([res], {

    //   type: "application/vnd.ms-excel;charset=utf-8",

    // });

    // const fileName = res.headers["content-disposition"].split("=")[1];

    const elink = document.createElement("a");

    elink.download = window.decodeURIComponent(name);

    elink.style.display = "none";

    elink.href = URL.createObjectURL(blob);

    document.body.appendChild(elink);

    elink.click();

    URL.revokeObjectURL(elink.href); // 释放URL 对象

    document.body.removeChild(elink);

  }

  /* 深拷贝 */

  export const deepClone = (obj) => {

    let result = typeof obj.splice === "function" ? [] : {};

    if (obj && typeof obj === 'object') {

        for (let key in obj) {

            if (obj[key] && typeof obj[key] === 'object') {

                result[key] = deepClone(obj[key]); //如果对象的属性值为object的时候,递归调用deepClone,即在吧某个值对象复制一份到新的对象的对应值中。

            } else {

                result[key] = obj[key]; //如果对象的属性值不为object的时候,直接复制参数对象的每一个键值到新的对象对应的键值对中。

            }

        }

        return result;

    }

    return obj;

}

导出vue页面引用

import { getDownload } from "@/utils/comp.js";

// 导出

    downLoadTemplete() {

      this.$http

        .getDownLoadRequest("/sso-management/user/downloadTemp", {})

        .then((res) => {

           //  引入直接调用,传入参数

          getDownload(res.data, "用户模板.xlsx");

        });

    },

export const getDownLoadRequest = (url, params,) => {

    return axios({

        method: 'get',

        url: `${url}`,

        responseType: 'blob', // 需要设置blob类型

        headers: {

            'Content-Type': 'application/json'

        },

        data: params

    });

};

  • 14
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
假设你使用axios库来发送请求,后端接口为/export,后端返回文件为Excel文件,可以按照以下步骤来实现: 1. 在Vue组件中定义一个方法,用于触发导出操作,并接收表单参数: ``` exportTable() { const params = { // 表单参数 } // 发送请求 axios({ url: '/export', method: 'POST', responseType: 'blob', data: params }).then(res => { // 处理返回文件 const content = res.data const blob = new Blob([content]) const fileName = 'export.xlsx' // 下载文件 const link = document.createElement('a') link.href = window.URL.createObjectURL(blob) link.download = fileName link.click() }).catch(error => { // 处理错误 }) } ``` 2. 在后端接口中,接收表单参数,生成Excel文件返回文件: ```python import io from flask import make_response import xlsxwriter @app.route('/export', methods=['POST']) def export(): # 接收表单参数 form_data = request.form # 生成Excel文件 output = io.BytesIO() workbook = xlsxwriter.Workbook(output) worksheet = workbook.add_worksheet() # 写入数据 workbook.close() output.seek(0) # 返回文件 response = make_response(output.read()) response.headers.set('Content-Type', 'application/vnd.ms-excel') response.headers.set('Content-Disposition', 'attachment', filename='export.xlsx') return response ``` 这样,当用户点击导出按钮时,前端会发送一个POST请求到后端的/export接口,后端会接收表单参数,生成Excel文件返回文件前端会处理文件下载Excel文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值