开发者

Special characters in node.js readdir()

开发者 https://www.devze.com 2023-04-12 05:53 出处:网络
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:

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

精彩评论

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

关注公众号