Home > Web Front-end > JS Tutorial > body text

Invalid encoding of nodejs iconv Description

高洛峰
Release: 2016-11-22 16:53:30
Original
1944 people have browsed it

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"))
});
Copy after login

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);
});
Copy after login


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!