Home  >  Article  >  Web Front-end  >  Invalid encoding of nodejs iconv Description

Invalid encoding of nodejs iconv Description

高洛峰
高洛峰Original
2016-11-22 16:53:301925browse

About encoding conversion, maybe you learned from the Internet that it is used in this way

var fs = require("fs");
var iconv=require("iconv-lite");
var request=require("request");
var txt=fs.readFileSync("./a.txt","utf8");
iconv.decode(txt,"gbk")

request("http://jd.com",function(err,res,body){
    console.log(iconv.decode(body,"gbk"))
});

The above output result is still garbled and cannot decode gbk. First, the latest iconv does not support string decoding.

Check the latest official documents. The following is the correct decoding method.

var fs = require("fs");
var iconv=require("iconv-lite");
var request=require("request");
var txt=fs.readFileSync("./a.txt");
iconv.decode(txt,"gbk")
request("http://jd.com")
.pipe(iconv.decodeStream('gbk'))
.collect(function(err, decodedBody) {
    fs.writeFile("./jd.txt",decodedBody);
});


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn