vue前端接受后台返回流数据,并实现文件正常下载

本文介绍了如何在Vue前端应用中接收后端(使用Java Spring和POI库)返回的流数据,从而实现Excel文件的正常下载。在Chrome浏览器中,文件将默认保存到特定路径,而其他浏览器则会弹窗让用户选择下载路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前端导出下载方法

 // 导出
      exporttable () {
        const apiUrl = "http://localhost:9999/export/telDetails";
        //console.log(this.form)
        let param = new URLSearchParams();
        param.append('startTime', this.addForm.startTime);
        param.append('endTime', this.addForm.endTime);
        this.$http.post(apiUrl, param, { responseType: 'blob' }).then((res) => {
          //console.log(res.data)
          const link = document.createElement('a')
          let blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })
          link.style.display = 'none'
          link.href = URL.createObjectURL(blob)
          let num = ''
          for (let i = 0; i < 10; i++) {
            num += Math.ceil(Math.random() * 10)
          }
          link.setAttribute('download', '话务详情表_' + num + '.xlsx')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)
        })
      },
      

 - data数据定义
 data () {
      return {
        addForm: {
          endTime: '',
          landingNumber: '',
          startTime: '',
          trunkNumber: '',
          called: '1',
          extension: '',
          direction: '',
          callResult: '',
          batchNumber: '',
          userData: '',
          seatNumber: '',
          calling: '',
          groupId: '',
          ringingTime: ''
          //trunkNumber:"2"
          /*verifiedAccount:'3434'  2020-08-03 00:00:00*/
        },
        dialogAddgsVisible: false,
        title: ''
      }
    },

后端导出下载代码


 - service层代码,此处是调用其他service方法获取数据,header跟临时文件存放路径需要自行修改
 package com.kaxin.appmanage.service.impl;

import com.kaxin.appmanage.pojo.TelDetails;
import com.kaxin.appmanage.service.ExportService;
import com.kaxin.appmanage.service.TelDetailsService;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;
@Service
public class ExportServiceImpl implements ExportService {

    //创建临时文件存放的路径
    private String temp = "c:\\temp\\excel\\";

    @Autowired
    private TelDetailsService telDetailsService;

    @Override
    public String exportStu(TelDetails telDetails) {

        List<TelDetails> list = telDetailsService.conditionalQuery(telDetails);
        //创建工作簿
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
        //创建工作表
        XSSFSheet sheet = xssfWorkbook.createSheet();
        xssfWorkbook.setSheetName(0, "话务统计表");
        //创建表头
        XSSFRow head = sheet.createRow(0);
        String[] heads = {"方向", "主叫号码", "被叫号码", "成功标志", "开始时间","结束时间","总时长","振铃时长","中继号","落地号"};
        for (int i = 0; i < 10; i++) {
            XSSFCell cell = head.createCell(i);
            cell.setCellValue(heads[i]);
        }
        for (int i = 1; i <= list.size(); i++) {
            TelDetails telDetails1 = list.get(i - 1);
            //创建行,从第二行开始,所以for循环的i从1开始取
            XSSFRow row = sheet.createRow(i);
            //创建单元格,并填充数据
            XSSFCell cell = row.createCell(0);
            cell.setCellValue(telDetails1.getDirection());
            cell = row.createCell(1);
            cell.setCellValue(telDetails1.getCalling());
            cell = row.createCell(2);
            cell.setCellValue(telDetails1.getCalled());
            cell = row.createCell(3);
            cell.setCellValue(telDetails1.getSuccessSign());
            cell = row.createCell(4);
           // SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //String dateString = dateFormat.format(telDetails1.getStartTime());
            cell.setCellValue(telDetails1.getStartTime());
            cell = row.createCell(5);
           // String dateString1 = dateFormat.format(telDetails1.getEndTime());
            cell.setCellValue(telDetails1.getEndTime());
            cell = row.createCell(6);
            cell.setCellValue(telDetails1.getTotalTime());
            cell = row.createCell(7);
            cell.setCellValue(telDetails1.getRingingTime());
            cell = row.createCell(8);
            cell.setCellValue(telDetails1.getTrunkNumber());
            cell = row.createCell(9);
            cell.setCellValue(telDetails1.getLandingNumber());
        }
        //创建临时文件的目录
        File file = new File(temp);
        if (!file.exists()) {
            file.mkdirs();
        }
        //临时文件路径/文件名
        String downloadPath = file + "\\" + System.currentTimeMillis() + UUID.randomUUID();
        OutputStream outputStream = null;
        try {
            //使用FileOutputStream将内存中的数据写到本地,生成临时文件
            outputStream = new FileOutputStream(downloadPath);
            xssfWorkbook.write(outputStream);
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return downloadPath;

    }
}

 - 设置样式需自行修改
 - [样式设置可参考](https://blog.csdn.net/qq_41793064/article/details/89415205?biz_id=102&utm_term=%E5%88%A9%E7%94%A8poi%E8%AE%BE%E7%BD%AEexcel%E6%A0%B7%E5%BC%8F&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-89415205&spm=1018.2118.3001.4187)

controller层代码,文件名需自己设置

package com.kaxin.appmanage.controller;

import com.kaxin.appmanage.pojo.TelDetails;
import com.kaxin.appmanage.service.ExportService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Controller
@CrossOrigin
@RequestMapping("/export")
public class ExportController {
    @Resource
    private ExportService exportService;

    @RequestMapping("/telDetails")
    public void exportStu(HttpServletResponse response, TelDetails telDetails) {
        //设置默认的下载文件名
        String name = "话务详情表.xlsx";
        try {
            //避免文件名中文乱码,将UTF8打散重组成ISO-8859-1编码方式 @RequestBody
            name = new String(name.getBytes("UTF8"), "ISO-8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //设置响应头的类型
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        //让浏览器下载文件,name是上述默认文件下载名
        response.addHeader("Content-Disposition", "attachment;filename=\"" + name + "\"");
        InputStream inputStream = null;
        OutputStream outputStream = null;
        //在service层中已经将数据存成了excel临时文件,并返回了临时文件的路径
        String downloadPath = exportService.exportStu(telDetails);
        //根据临时文件的路径创建File对象,FileInputStream读取时需要使用
        File file = new File(downloadPath);
        try {
            //通过FileInputStream读临时文件,ServletOutputStream将临时文件写给浏览器
            inputStream = new FileInputStream(file);
            outputStream = response.getOutputStream();
            int len = -1;
            byte[] b = new byte[1024];
            while ((len = inputStream.read(b)) != -1) {
                outputStream.write(b);
            }
            //刷新
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭输入输出流
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
        //最后才能,删除临时文件,如果流在使用临时文件,file.delete()是删除不了的
        file.delete();

    }
}

注意:谷歌浏览器是默认下载路径的,需要自己设置要自行修改,换其他浏览器会自动弹窗,用户可自选下载路径

前端进行文件下载,通常是通过调用后台接口获取文件流,然后在浏览器端触发一个下载操作。使用Vue框架时,可以通过axios等HTTP请求库来获取后台数据,再配合一些前端JavaScript代码来实现文件下载。以下是一个简单的示例实现: ```javascript <template> <button @click="downloadFile">下载文件</button> </template> <script> export default { methods: { downloadFile() { // 使用axios发送请求,获取文件流 axios({ url: '后台接口地址', // 请替换为实际的接口地址 method: 'GET', responseType: 'blob', // 告诉浏览器响应体是blob类型,这样文件下载时才能正确处理 timeout: 10000 }).then(response => { // 创建一个URL对象,用于读取响应的blob数据 const url = window.URL.createObjectURL(new Blob([response.data])); // 创建一个a标签用于下载 const link = document.createElement('a'); link.href = url; link.setAttribute('download', '文件名.xlsx'); // 设置下载文件名,可以包含后缀 // 把a标签添加到文档中,模拟点击实现下载 document.body.appendChild(link); link.click(); // 下载完成后移除a标签 document.body.removeChild(link); }).catch(error => { console.error('下载文件失败:', error); }); } } }; </script> ``` 在这个示例中,点击按钮会触发`downloadFile`方法。这个方法通过axios向后台请求文件数据,设置`responseType`为`blob`以确保响应数据被作为二进制数据处理。获取到文件流后,使用`window.URL.createObjectURL`创建一个可下载的URL,然后创建一个`a`标签,设置其`href`属性为这个URL,通过设置`download`属性指定下载文件名。最后模拟点击`a`标签触发下载下载后将其从文档中移除。 请注意,根据实际需求,后台接口地址、请求方式、文件名以及请求参数等都需要根据实际情况进行调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值