Node Express.js - Download file from memory - 'filename must be a string'

前端 未结 2 749
悲&欢浪女
悲&欢浪女 2021-02-07 05:00

I\'m trying to package data in memory into a text file and send it to the user, triggering a file download.

I have the following code:

app.get(\'/downloa         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-07 05:33

    app.get('/download', (request, response) => {
      const fileData = 'SGVsbG8sIFdvcmxkIQ=='
      const fileName = 'hello_world.txt'
      const fileType = 'text/plain'
    
      response.writeHead(200, {
        'Content-Disposition': `attachment; filename="${fileName}"`,
        'Content-Type': fileType,
      })
    
      const download = Buffer.from(fileData, 'base64')
      response.end(download)
    })
    

提交回复
热议问题