javascript - 如何在javascript中将文件从base64重建为pdf?
问题描述
在 Nodejs 中,我收到了来自 api 的响应
{
"file": "PHN0eWxlPnRlRrU3VRbUNDJyAvPjwvcD4K",
"mime_type": "text/html",
"document_type": "shippingLabel"
}
要重建文件,需要对来自节点的数据进行 base64 解码,并根据 mime_type 进行解释。
帮助我获取文件.pdf
并保存到目录。
解决方案
使用fs.writeFileSync(file, data[, options])
:
const fs = require('fs');
// get your response somehow...
const response = {
file: 'PHN0eWxlPnRlRrU3VRbUNDJyAvPjwvcD4K',
mime_type: 'text/html',
document_type: 'shippingLabel'
};
// LUT for MIME type to extension
const ext = {
'text/html': 'html',
// ...
}
// save to shippingLabel.html
fs.writeFileSync(`${response.document_type}.${ext[response.mime_type]}`, response.file, 'base64');
推荐阅读
- c# - 将 xml 字符串属性映射到 C# 属性
- c++ - 在我的 Mac Qt 项目中找不到“fbxsdk.h”文件,如何在我的 Mac Qt 项目中添加 FBX SDK?
- pyspark - 调用 pyspark.ml.feature 的 PCA 时 Py4JJava 错误列错误
- r - 在ggplot中绘制多条线
- javascript - 绑定导入的函数上下文
- java - 是什么阻止了某人通过伪装成他们不是的模块来破坏合格的导出?
- php - 如何获取 Laravel 集合中元素的索引
- ios - 如何冻结视图直到某个值为真?
- c# - 如何从两个 DateTimePickers 中获取时间长度并与整数进行比较?
- android - ADMOB 插页式广告在 real-unit-id 中不起作用,即使它适用于 test-unit-id