search

Home  >  Q&A  >  body text

javascript - node capture, the image download is incomplete

Using the node request module to capture images in batches, I found that some images were not downloaded completely. Some of the images were displayed normally and some were gray.
Moreover, incomplete downloading of pictures also occurs randomly, not necessarily which one.
code show as below:

var arr=["http:www.a.com/1.png","http:www.a.com/2.png","http:www.a.com/3.png"....]
for(var i=0;i<30;i++){
    lodPic(arr[i])
}
function loadPic(url){
    var _url=encodeURI(url);
    request.head(_url,function(err,res,body){
        if(err){
            console.log(err);
        }
        request(url).pipe(fs.createWriteStream(ph)).on('close',function(){
            console.log('finfish');
        });;
    });
}
迷茫迷茫2750 days ago760

reply all(3)I'll reply

  • 仅有的幸福

    仅有的幸福2017-05-16 13:30:49

    var request = require('request');
    
    var options = {
      url: 'https://api.github.com/repos/request/request',
      headers: {
        'content-length': 1024,
      }
    };
    
    function callback(error, response, body) {
        //
    }
    
    request(options, callback);

    Look at the code and try setting the content-length to a larger value. I have encountered this before when writing a static server using cpp. Some pictures were larger than this value, so only half of them were successfully transferred. Some pictures are smaller than this value, so they can be transmitted completely. I don't know if you have this problem too.

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-16 13:30:49

    Maybe there is something wrong with 30 concurrent writes to the disk. Can you try reducing the number of concurrent writes?
    Or try parallelLimit() of async.js?

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-16 13:30:49

    The HEAD method of http has no response body. If you use this http method to request images, you will definitely get nothing.

    reply
    0
  • Cancelreply