node.js - nodejs script not downloading files - Stack Overflow

I have a nodejs script (v. 22.13.1) on a computer with Ubuntu 24.04. It uses puppeteer to connect to a

I have a nodejs script (v. 22.13.1) on a computer with Ubuntu 24.04. It uses puppeteer to connect to a website, get a list of images, and is supposed to download and save the images locally. The first two parts that connect to the site and get the list of images work perfectly. It's the downloading and saving that isn't working.

I am using a commonly-found method for downloading and saving the images found both here on stackoverflow and many blogs. It looks like this:

for(var x=0;x<card_arr.length;x++)
{
    var key = card_arr[x]
    const file = fs.createWriteStream(gp_args['local_path_start'] + '/' + key + '.jpg');
    console.log('getting ' + gp_args['local_path_start'] + '/' + key + '.jpg')
    console.log('from ' + gp_args['url'] + 'img/' + key + '.jpg')
    http.get(gp_args['url'] + 'img/' + key + '.jpg', res => {
        // Write data into local file
        res.pipe(file)

        // Close the file
        file.on('finish', () => {
          file.close()
          console.log(`File downloaded!`)
        })
    })
        .on('error', err => {
        console.log('Error: ', err.message)
    })

    var milliseconds = 300
    var waitTill = new Date(new Date().getTime() + milliseconds);
    while(waitTill > new Date()){}
}

My output looks similar to this:

getting images/remotesite/MWSTX1CKI02.jpg
from .jpg
getting images/remotesite/MWSTX1CKE01.jpg
from .jpg
getting images/remotesite/MWSTX1CKC01.jpg
from .jpg
getting images/remotesite/MWSTX1CKQ03.jpg
from .jpg
getting images/remotesite/MWSTX1CKC02.jpg
from .jpg
getting images/remotesite/MWSTX1CKQ05.jpg
from .jpg

However, when I check the folder images/remotesite/ on the local computer the folder is empty. I get no error messages, no "File downloaded!" messages, nothing other than that output. I verified that the links are correct by going to a few of them in the browser, and they load just fine. Is there something else it needs?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745252062a4618736.html

相关推荐

  • node.js - nodejs script not downloading files - Stack Overflow

    I have a nodejs script (v. 22.13.1) on a computer with Ubuntu 24.04. It uses puppeteer to connect to a

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信