美文网首页
发送post请求下载文件

发送post请求下载文件

作者: 叫我苏轼好吗 | 来源:发表于2018-12-11 16:07 被阅读0次

发送post请求下载文件

先说一下背景:这是一个以vue作为框架并用Axios来发送http请求的项目。我想要实现用axios来发送post请求,然后服务器会返回的response是一个文件流,我希望能将这个文件流写入excel,从而实现该excel文件的下载。

在网上查阅了相关资料后,我在Axios官方文档给出的一个不大完整的示例中看到一种基于node原生模块fs的处理方案,我考虑到是否可以采用这种方式来处理我接受到的文件流,可是经过尝试,发现行不通。

以下是我亲试可以实现的一种方案:

exportData () {

        const form = this.getSearchForm() // 要发送到后台的数据

        axios({ // 用axios发送post请求

          method: 'post',

          url: '/user/12345', // 请求地址

          data: form, // 参数

          responseType: 'blob' // 表明返回服务器返回的数据类型

        })

          .then((res) => { // 处理返回的文件流

            const content = res

            const blob = new Blob([content])

            const fileName = '测试表格123.xls'

            if ('download' in document.createElement('a')) { // 非IE下载

              const elink = document.createElement('a')

              elink.download = fileName

              elink.style.display = 'none'

              elink.href = URL.createObjectURL(blob)

              document.body.appendChild(elink)

              elink.click()

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

              document.body.removeChild(elink)

            } else { // IE10+下载

              navigator.msSaveBlob(blob, fileName)

            }

        })

      }

相关文章

  • python3-requests发送不同类型的接口请求

    发送get请求 发送post请求 上传文件接口 下载文件接口

  • AFN 基本使用

    GET&POST 请求 文件下载 文件上传 序列化处理 监听网络状态 GET 方式发送请求 POST方式发送请求 ...

  • python+requests+pytest接口自动化

    1、发送get请求 2、发送post请求 3、发送https请求 4、文件上传 5、文件下载 6、timeout超...

  • JS实现点击按钮下载文件

    下载项目中的文件 目录 1.下载项目中的文件 2.发送请求地址下载文件 1.get请求 2.post请求 1.下载...

  • 使用requests发送请求-01

    发送get请求 发送post请求 上传文件 携带cookie发送请求 使用session发送请求

  • 发送post请求下载文件

    发送post请求下载文件 先说一下背景:这是一个以vue作为框架并用Axios来发送http请求的项目。我想要实现...

  • curl常用的命令

    发送单个请求,将结果输出到控制台 将文件下载到本地 查询单词的含义 发送POST请求 自定义Header的请求 除...

  • NSURLSession

    NSURLSession 发送一般的GET和POST请求 下载文件不需要离线断点(小文件) 下载文件需要离线断点(...

  • Axios发送post请求下载文件

    发送post请求下载文件 先说一下背景:这是一个以vue作为框架并用Axios来发送http请求的项目。我想要实现...

  • AFNetworking使用

    get请求方式 post请求方式一 post请求方式二 下载文件

网友评论

      本文标题:发送post请求下载文件

      本文链接:https://www.haomeiwen.com/subject/ielehqtx.html