Q: nodejs (using egg) return .zip file failed I wrote a Nodejs service(using egg) to return a local .zip file (which is zipped by a directory) to browser,but it failed. the code shows as below
egg.js
 // zip file by code or manually

 async download() {
    const ctx = this.ctx;
    const params = ctx.request.body;
    const downloadrepoName = params.fileName;
    const filePath = path.resolve(__dirname, `../../public/${downloadrepoName}`);
    // ctx.attachment(`${downloadrepoName}`);
    const stats = fs.statSync(filePath);
    ctx.response.set({
      'Content-Type': 'application/octet-stream',
      'Content-Disposition': `attachment; filename=${downloadrepoName}`,
      'Content-Length': stats.size,
    });
    ctx.body = fs.createReadStream(filePath);
  }


front code

 
  // ajax header
  config.headers['responseType'] = 'blob'
  config.headers['Accept'] = 'application/zip'


 downloadFrontProject({ fileName }).then(res => {
            // 使用插件,第二个参数是文件名
        console.log(res)
        const blob = new Blob([res])

        const linkNode = document.createElement('a')

        linkNode.download = fileName // a标签的download属性规定下载文件的名称
        linkNode.style.display = 'none'
        linkNode.href = URL.createObjectURL(blob) // 生成一个Blob URL
        document.body.appendChild(linkNode)
        linkNode.click() // 模拟在按钮上的一次鼠标单击

        URL.revokeObjectURL(linkNode.href) // 释放URL 对象
        document.body.removeChild(linkNode)
     })

looks like the message shows it's not a zip file, but all the directory infos

.zip on service is 385k,but when I download is a broken 686k .zip

all the headers


besides, if i returned a .text file ,it worked fine。
as a node beginner ,needs some help

A: import axios from 'axios'
const request = axios.create({
  baseURL: process.env.VUE_APP_BASE_API_WX,
  timeout: 10000
})
 request({
          url: '/api/frontscaffold/download',
          method: 'post',
          data: {
            fileName: fileName
          },
          responseType: 'blob'
          
        }).then((res) => {
          console.log('res', res)
          const blob = res.data
          const reader = new FileReader()
          reader.readAsDataURL(blob)
          reader.onload = (e) => {
            const a = document.createElement('a')
            a.download = fileName
            a.href = e.target.result
            document.body.appendChild(a)
            a.click()
            document.body.removeChild(a)
          }
        })

when I wrote like above, all works well...still don't get it
