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

How to download pictures in node.js

亚连
Release: 2018-06-02 09:25:26
Original
2104 people have browsed it

This article introduces you to the 2 ways to download images in node.js and the implementation code for downloading remote images through example code. It is very good and has reference value. Friends who need it can refer to it

Specific code As shown below:

var request=require("request");
var fs=require("fs");
function download1(url,filename,fn){
request(url).pipe(fs.createWriteStream(filename).on("close",function(err,res){
if(err){
console.log(err);
}else{
fn&&fn();
}
}))
}
function download2(url,filename,fn){
request.get({uri:url, encoding:'binary'},function(err,res){
if(!err){
fs.writeFile(filename,res.body,"binary",function(err,res){
if(!err){
fn&&fn();
}else{
console.log(err);
}
})
}
})
}
Copy after login

ps: Let’s take a look at the implementation code of nodejs downloading remote images. The specific code is as follows:

var express = require('express');
var request = require('request');
var http = require('http');
var url = require('url');
var fs = require("fs");
var router = express.Router();
/* GET home page. */
router.get('/', function (req, res, next) {
  var url = "http://www.valu.cn/images/1.gif";
  //request('http://www.valu.cn/images/1.gif').pipe(fs.createWriteStream('./public/upload/downImg/logonew.png'));
  var req = http.get(url, function (res) {
    var imgData = "";
    res.setEncoding("binary"); //一定要设置response的编码为binary否则会下载下来的图片打不开
    res.on("data", function (chunk) {
      imgData += chunk;
    });
    res.on("end", function () {
      fs.writeFile("./public/upload/downImg/logonew.png", imgData, "binary", function (err) {
        if (err) {
          console.log("保存失败");
        }
        console.log("保存成功");
      });
    });
    res.on("error", function (err) {
      console.log("请求失败");
    });
  });
  req.on('error', function (err) {
    console.log("请求失败2" + err.message);
  });
  res.render('index', {title: '首页2'});
});
module.exports = router;
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Detailed explanation of the javascript module loader through code examples

Implemented through Vue using v-for Method of assigning value to src attribute (detailed tutorial)

How to load local static images through v-for in vue (detailed tutorial)

The above is the detailed content of How to download pictures in node.js. For more information, please follow other related articles on the PHP Chinese website!

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!