Node.js - 下载本地 docx 或 pdf 编码为 base64 字符串
Node.js - Download locally docx or pdf encoded as base64 string
我想将 docx 或 pdf 本地存储在我的 node.js 项目的文件夹中。这些文档被编码为 Base64 字符串,但它们具有特定的媒体类型。因此,当我尝试以常规方式写入文件时,如下所示,结果是不可读的。
const express = require("express");
const router = express.Router();
const fs = require('fs');
router.post("/evaluate", function(req, res) {
var oResume = req.body.resume;
var sMimeType = oResume.mimeType;
var sFileExtension = oResume.fileExtension;
var sBase64 = oResume.content.replace(/(\r\n|\n|\r)/gm, "");
fs.writeFile("cvs/out."+sFileExtension, sBase64, 'base64', function(err) {
console.log(err);
});
res.status(200).send("OK");
});
如何存储保持正确格式的文档?
我已经用这个解决方案解决了:
const express = require("express");
const atob = require("atob");
const router = express.Router();
const fs = require('fs');
router.post("/evaluate", function(req, res) {
var oResume = req.body.resume;
var sMimeType = oResume.mimeType;
var sFileExtension = oResume.fileExtension;
var sBase64 = oResume.content.replace(/(\r\n|\n|\r)/gm, "");
var sBinaryString = atob(sBase64);
var len = sBinaryString.length;
var aBytes = new Uint8Array(len);
for (var i=0; i<len; i++) {
aBytes[i] = sBinaryString.charCodeAt(i);
}
var blob = Buffer.from(aBytes.buffer);
fs.writeFile("cvs/out."+sFileExtension, blob, function(err) {
console.log(err);
});
res.status(200).send("OK");
});
module.exports = router;
我想将 docx 或 pdf 本地存储在我的 node.js 项目的文件夹中。这些文档被编码为 Base64 字符串,但它们具有特定的媒体类型。因此,当我尝试以常规方式写入文件时,如下所示,结果是不可读的。
const express = require("express");
const router = express.Router();
const fs = require('fs');
router.post("/evaluate", function(req, res) {
var oResume = req.body.resume;
var sMimeType = oResume.mimeType;
var sFileExtension = oResume.fileExtension;
var sBase64 = oResume.content.replace(/(\r\n|\n|\r)/gm, "");
fs.writeFile("cvs/out."+sFileExtension, sBase64, 'base64', function(err) {
console.log(err);
});
res.status(200).send("OK");
});
如何存储保持正确格式的文档?
我已经用这个解决方案解决了:
const express = require("express");
const atob = require("atob");
const router = express.Router();
const fs = require('fs');
router.post("/evaluate", function(req, res) {
var oResume = req.body.resume;
var sMimeType = oResume.mimeType;
var sFileExtension = oResume.fileExtension;
var sBase64 = oResume.content.replace(/(\r\n|\n|\r)/gm, "");
var sBinaryString = atob(sBase64);
var len = sBinaryString.length;
var aBytes = new Uint8Array(len);
for (var i=0; i<len; i++) {
aBytes[i] = sBinaryString.charCodeAt(i);
}
var blob = Buffer.from(aBytes.buffer);
fs.writeFile("cvs/out."+sFileExtension, blob, function(err) {
console.log(err);
});
res.status(200).send("OK");
});
module.exports = router;