0

我在使用 NodeJS“shred”在 POST 请求中提交 UTF8 编码的文本文件时遇到问题

我试图发布的文本内容在客户端看起来很好,我知道因为我在调用 client.post 之前将它控制台记录到屏幕上,服务器得到的是文本文件的内容,但最后 2 个字符是总是丢失/切碎。这不是 ANSI 文本文件的问题。如果我将文本文件从 UTF8 转换为 ANSI,当它到达服务器时它就完成了。

var Shred = require('shred');
var client = new Shred();
var textToPost = fs.readFileSync("myfile.txt", 'utf8');
console.log (textToPost);
client.post({
     url: "http://www.example.com/readTextFile.php",
     headers: { 'Content-Type': 'application/x-subrip'},
content: textToPost,
on: {
  200: function (response) {
    console.log("posted ok");
console.log(response.content.body);
  },
  500: function (response) {
    asyncCb(new Error('bad response\n' + response.content.body));
  }
}

服务器(通过 readTextFile.php)收到的是 myfile.txt 的内容,其中最后 2 个字符被删除。我不明白为什么。这对下游有很大的影响,因此任何零散的解决方法都不太可能有帮助。

我还注意到,当 textToPost 的内容被记录到控制台时,有一个“?” 在内容之前。当文件是 ANSI 编码文件时,这不会出现。

请帮忙。。谢谢

4

1 回答 1

0

Ok, after the comments above (thanks rdrey and JohnnyHK), I decided to try stripping out the BOM from the file(s). So I used a hex editor, deleted the EF BB BF chars and saved, this time the file arrived at the server fully intact with no chars missing at the end. Now I'll modify my nodeJS to strip the chars out also. This doesn't completely answer my question (why is there an issue with the BOM). Perhaps shred has a problem posting text files with a BOM. Maybe it incorrectly reads it and decides the file is smaller than it actually is and as a result, chops the end off. I am not sure.

于 2012-08-30T08:11:01.917 回答