Express.js - 如何下载base64字符串作为PDF文件?

Tom*_*mas 12 base64 file stream node.js express

我有pdf文件编码为base64字符串.如何将此字符串作为.pdf格式的文件下载到浏览器?

我已经尝试过的:

res.set('Content-Disposition', 'attachment; filename="filename.pdf"');
res.set('Content-Type', 'application/pdf');

res.write(fileBase64String, 'base64');
Run Code Online (Sandbox Code Playgroud)

ofh*_*use 11

我最终先解码pdf,然后将其作为二进制文件发送到浏览器,如下所示:

(为简单起见,我node-http在这里使用,但功能也可用express)

const http = require('http');

http
  .createServer(function(req, res) {
    getEncodedPDF(function(encodedPDF) {
      res.writeHead(200, {
        'Content-Type': 'application/pdf',
        'Content-Disposition': 'attachment; filename="filename.pdf"'
      });

      const download = Buffer.from(encodedPDF.toString('utf-8'), 'base64');

      res.end(download);
    });
  })
  .listen(1337);
Run Code Online (Sandbox Code Playgroud)

让我疯狂的是Postman的测试:
我使用SendButton代替Send and Download Button来提交请求:在此输入图像描述

但是使用Send此请求的按钮会导致pdf文件在保存后损坏.