2016-11-25 78 views
1

有任何一個你想上傳50+圖像cloundinary?我一直在嘗試,但問題在於承諾沒有得到解決(即使使用.reflect()也無法上傳所有圖像。根據上傳速度,它將失敗30%〜70%。cloudinary多種JPG格式上傳的Node.js

有沒有方法徹底讓它異步,並確保所有圖像都正確上傳?只有我使用的是從他們的文檔的藍鳥和cloudinary模塊模塊。

Promisebd = require('bluebird'); 
 

 
function uploadimg(img, i, itemId) { 
 
    var pubID = 'a2z/toys/' + itemId + '/' + i; 
 
    // return new cloudImg.v2.uploader.upload(img, { 
 
    return cloudinary.v2.uploader.upload(img, { // works 
 
    public_id: pubID, 
 
    quality: 90 
 
    // use_filename: true, 
 
    } , function(err, res){ 
 
    if(err) { 
 
     console.log(err); 
 
    } 
 
    console.log(res); 
 
    }); 
 
} 
 

 

 
promiseArr.push(uploadimg(fullimg, i, d[0].details.detailsProductId)); // pushing the promises to Arr 
 

 
    Promisebd.all(promiseArr.map(function(promise) { 
 
      return promise.reflect(); 
 
      })).each(function(inspection) { 
 
      if(inspection.isFulfilled()) { 
 
       console.log('The promise is the arr was fulfilled with ', inspection.value()); 
 
      }else{ 
 
       console.log('The promise is NOT the arr was NOT fulfilled with ', inspection.reason()); 
 
      } 
 
      })

回答

1

promsify上傳IMG功能,並嘗試使用它

function uploadimgAsync(img, i, itemId) { 
    return new Promise(function (resolve, reject) { 
     var pubID = 'az/toys/' + itemId + '/' + i; 
     cloudinary.v2.uploader.upload(img, { // works 
      public_id: pubID, 
      quality: 90 
     }, 
     function(err, res){ 
      if(err) { 
       reject(err); 
      } 
      resolve(res); 
     }); 
    }); 
}