......
let fileCounter = 0;
const
zippedFilename = encodeURIComponent(downloadData.name);
const
list = downloadData.list || [];
const
header = {
'Content-Type'
:
'application/x-zip'
,
'Pragma'
:
'public'
,
'Expires'
:
'0'
,
'Cache-Control'
:
'private, must-revalidate, post-check=0, pre-check=0'
,
'Content-disposition'
:
'attachment; filename="'
+ zippedFilename +
'"'
,
'Transfer-Encoding'
:
'chunked'
,
'Content-Transfer-Encoding'
:
'binary'
};
res.writeHead(200, header);
archive.store = true;
archive.pipe(res);
list.map(item => {
fileCounter++;
let inStream = request.get(item.downLoadUrl);
let name = item.fileName;
let length = 0;
inStream.on(
'response'
,
function
(awsData) {
archive.append(inStream, {
name: name
});
}).on(
'data'
,
function
(data) {
length += data.length;
}).on(
'error'
,
function
(e) {
console.error(name +
'-error'
, e);
}).on(
'end'
,
function
(endData) {
fileCounter--;
if
(fileCounter < 1) {
archive.finalize();
}
});
});
archive.on(
'error'
,
function
(err) {
throw
err;
});
archive.on(
'finish'
,
function
(err) {
return
res.
end
();
});
......