【问题标题】:Save file from url base64 data to pdf将文件从 url base64 数据保存为 pdf
【发布时间】:2021-04-19 20:11:57
【问题描述】:

我正在制作一个反应应用程序,其中有一个允许用户上传 PDF 文件的字段。

我已成功将文件作为 base64 字符串上传并发送到服务器,我确实收到了,但是我无法将文件保存回 pdf,这是我尝试过的:

const fs = require("fs");
const invoice = { fileData: "data:application/pdf;base64,JVBERi0xLjandtherestofthedatastring..." };

const invoiceFileContents = new Buffer.from(invoice.fileData, "base64");
fs.writeFileSync(__dirname + "invoicetest.pdf", invoiceFileContents);

这确实会创建一个 pdf 文件,但我无法打开它,Adobe 说它坏了。

【问题讨论】:

    标签: node.js pdf fs


    【解决方案1】:

    我设法解决了,整个数据字符串data:application/pdf;base64,前面的附加字符串应该被修剪:

    const invoiceFileContents = new Buffer.from(
        invoice.fileData.substring(invoice.fileData.indexOf("base64") + 7),
        "base64"
      );
    

    【讨论】:

      猜你喜欢
      • 2020-07-02
      • 1970-01-01
      • 2022-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-04
      • 1970-01-01
      相关资源
      最近更新 更多