spring boot axios导出后缀是doc,docx文件

前端html文件

html文件

<template>
    <div>
        <el-button type="primary" @click="doc03">doc</el-button>
        <el-button type="primary" @click="doc07">docx</el-button>
    </div>
</template>

<script>
    import pageApi from '../../../../Api'
    export default {
        name: "filedown",
        methods:{
               
            doc07(){
                pageApi.doc07().then(res => { //res为axios请求返回的未被本地代码处理过的对象
                    debugger
                    const url = window.URL.createObjectURL(new Blob([res.data]))
                    const link = document.createElement('a')
                    link.href = url
                    link.setAttribute('download', `1.docx`)
                    document.body.appendChild(link)
                    link.click()
                })

            },
            doc03(){
                pageApi.doc03().then(res => { //res为axios请求返回的未被本地代码处理过的对象
                    debugger
                    const url = window.URL.createObjectURL(new Blob([res.data]))
                    const link = document.createElement('a')
                    link.href = url
                    link.setAttribute('download', `1.doc`)
                    document.body.appendChild(link)
                    link.click()
                })

            }
        }
    }
</script>

<style scoped>
</style>

api 文件

api文件里面的重点是要设置responseType,设置的方式为:
let myAxios = axios.create({
responseType: ‘arraybuffer’
})

 doc07(){
    return new Promise(function (resolve, reject) {
      let myAxios = axios.create({
        responseType: 'arraybuffer'

      })
      myAxios.post('/DownloadController/doc07', {})
          .then((res) => {
            resolve(res)
          })
          .catch((error) => {
            reject(error)
          })
    })
  },
  doc03(){
    return new Promise(function (resolve, reject) {
      let myAxios = axios.create({
        responseType: 'arraybuffer'

      })
      myAxios.post('/DownloadController/doc03', {})
          .then((res) => {
            resolve(res)
          })
          .catch((error) => {
            reject(error)
          })
    })
  }

后端方法

下面只截取了方法,具体类随便创建,不影响。

 @CrossOrigin(origins = "http://localhost:8080")
    @RequestMapping(value = "/doc07", method = { RequestMethod.POST,RequestMethod.GET}) // postman,url,3.tbox请求下载文件,暂时只支持单文件下载。
    public void getdocl07(HttpServletRequest request, HttpServletResponse response){
        String fileName ="1.docx";
        InputStream inputStream = null;
        ServletOutputStream servletOutputStream = null;
        try {
            String path = "source" + File.separator + fileName;
            org.springframework.core.io.Resource resource = resourceLoader.getResource("classpath:"+path);
            response.setContentType("application/octet-stream");
            response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            response.addHeader("Pragma", "no-cache");

            inputStream = resource.getInputStream();
            servletOutputStream = response.getOutputStream();
            IOUtils.copy(inputStream, servletOutputStream);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (servletOutputStream != null) {
                    servletOutputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
                System.gc();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @CrossOrigin(origins = "http://localhost:8080")
    @RequestMapping(value = "/doc03", method = { RequestMethod.POST,RequestMethod.GET}) // postman,url,3.tbox请求下载文件,暂时只支持单文件下载。
    public void getdocl03(HttpServletRequest request, HttpServletResponse response){
        String fileName ="1.doc";
        InputStream inputStream = null;
        ServletOutputStream servletOutputStream = null;
        try {
            String path = "source" + File.separator + fileName;
            org.springframework.core.io.Resource resource = resourceLoader.getResource("classpath:"+path);
            response.setContentType("application/octet-stream");
            response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            response.addHeader("Pragma", "no-cache");

            inputStream = resource.getInputStream();
            servletOutputStream = response.getOutputStream();
            IOUtils.copy(inputStream, servletOutputStream);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (servletOutputStream != null) {
                    servletOutputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
                System.gc();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

运行结果

doc文件

在这里插入图片描述
docx文件
在这里插入图片描述
想看具具体设置请看上一篇文章:导出csv,xls,xlsx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值