nodejs 下载远程文件
2019-02-11
注意: 需要添加res.setEncoding(“binary”);
var http = require('http'); var fs = require('fs'); var url = ""; //-- 目标地址 var req = http.get(url,function (res) { var fileData =""; res.setEncoding("binary"); console.log("loading..."); res.on("data",function (chunk) { fileData += chunk; }); res.on("end",function () { //-- 本地文件路径 fs.writeFile("D:/ttt/1/newFils", fileData, "binary", function (err) { if (err) { return;console.log("failed to save"); } console.log("success to save"); }); }); res.on("error",function (err) { console.log("failed to load"); }); });
By Pury.