Node.js copy remote file to server

前端 未结 3 2042
南旧
南旧 2020-12-11 20:33

Right now I\'m using this script in PHP. I pass it the image and size (large/medium/small) and if it\'s on my server it returns the link, otherwise it copies it from a remot

3条回答
  •  情书的邮戳
    2020-12-11 21:13

    To give a more updated version (as the most recent answer is 4 years old, and http.createClient is now deprecated), here is a solution using the request method:

    var fs = require('fs');
    var request = require('request');
    function getImage (img, size, filesize) {
        var imgPath = size + '/' + img + '.jpg';
        if (filesize) {
            return './images/' + imgPath;
        } else {
            request('http://www.othersite.com/images/' + imgPath).pipe(fs.createWriteStream('./images/' + imgPath))
            return './images/' + imgPath;
        }
    }
    

提交回复
热议问题