我使用下面的代码将文件编码为 Base64。
var bitmap = fs.readFileSync(file);
return new Buffer(bitmap).toString('base64');
我认为在文件中我们有问题“”
and ‘’
字符,但它很好"
当我们有It’s
,节点对字符进行编码,但是当我解码时,我将其视为It’s
这是我用来解码的 JavaScript:
fs.writeFile(reportPath, body.buffer, {encoding: 'base64'}
因此,一旦文件被编码和解码,它就无法与这些时髦的字符一起使用 -It’s
有人能解释一下吗?
这应该有效。
示例脚本:
const fs = require('fs')
const filepath = './testfile'
//write "it's" into the file
fs.writeFileSync(filepath,"it's")
//read the file
const file_buffer = fs.readFileSync(filepath);
//encode contents into base64
const contents_in_base64 = file_buffer.toString('base64');
//write into a new file, specifying base64 as the encoding (decodes)
fs.writeFileSync('./fileB64',contents_in_base64,{encoding:'base64'})
//file fileB64 should now contain "it's"
我怀疑你的原始文件没有utf-8编码,查看你的解码代码:fs.writeFile(reportPath, body.buffer, {encoding: 'base64'})
我猜测您的内容来自某种 http 请求,因此该内容可能不是 utf-8 编码的。看看这个:https://www.w3.org/International/articles/http-charset/index https://www.w3.org/International/articles/http-charset/index如果未指定字符集 Content-Type text/ 使用 ISO-8859-1。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)