node.js中的特殊字符readdir() [英] Special characters in node.js readdir()
问题描述
我正在node.js中运行这段代码,以便查看目录中的文件并查看其统计信息:
I'm running this piece of code in node.js in order to see the files in a directory an to see the stats for them:
var getFiles = function (dir, done) {
fs.readdir(dir, function (err, files) {
if (err) return done(err);
var pending = files.length;
files.forEach(function (file) {
fullPath = dir + "/" + file;
console.log(fullPath);
fs.stat(fullPath, function (err, stat) {
if (err) {
console.log("Stat error");
} else if (stat && stat != undefined) {
console.log("Success");
}
});
});
});
}
我的问题是文件名包含特殊字符.我是瑞典人,所以有很多å,ä和ö. fullPath的输出在输出大多数文件名时是正确的,但是只要文件名包含特殊字符,该字符就会显示为?",然后fs.stat失败,原因是它找不到文件.我错过了什么?我正在Windows上运行v0.5.7版本.
预先感谢.
My problem is with file names containing special characters. I'm swedish, so there are lots of å, ä and ö. The output from fullPath is correct when it's outputting most filenames, but whenever the filename contains a special character, that character is displayed as "?", and then fs.stat fails cause it cannot find the file. What have I missed? I'm running version v0.5.7 on Windows.
Thanks in advance.
推荐答案
我认为问题是Windows将文件名编码为ISO,但是节点将其读取为utf8.尝试使用 iconv 将iso转换为utf8.
I think that the problem is that Windows encodes filenames as ISO-whatever but node reads them as utf8. Try using iconv to convert from iso to utf8.
这篇关于node.js中的特殊字符readdir()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!