开发者

Special characters in node.js readdir()

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) {
 开发者_如何学Go   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");
        }
      });
    });
  });
}

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜