我正在下载一个pdf,它被分成服务器中的png。当我只使用一个承诺时,我设法单独下载一个页面,但是在链接这些承诺时它不起作用,所以我可以全部下载它们。当它到达“bucket.upload”位时会抛出“没有这样的文件”错误。同样,如果我不链接承诺,它会顺利运行,但我需要它来处理非线性页数。
我确定循环没有做好。
const path = require('path');
const os = require('os');
const fs = require('fs');
const request = require('request');
mkdirp('tmp').then(() => {
var pages = 5;
let promises = [];
for (var i = 1; i <= pages; i++) {
promises.push(getPdfPage(i));}
return Promise.all(promises)}).then(() => {
console.log('All files created');
// Uploading the file.
return bucket.upload('tmp/page1.png'), { destination: 'newupload.png', metadata: metadata });
});
function getPdfPage(p) {
var aux = 'tmp/page.png';
return new Promise(resolve => {request('http:www.example.com/image'+ p + '.png', function (error, response, body) {
// adding page number to file
var chars = aux.split('');
chars.splice(-4, 0, p);
aux = chars.join('');
}).pipe(fs.createWriteStream(aux)).on('finish', resolve);})
}
提前谢谢!