无法下载动态创建的 ZIP 存档
Can't download dynamically created ZIP archive
我正在尝试将 MySQL 查询的输出动态写入存档。这是我的代码:
var async = require("async");
var mysql = require("mysql");
var express = require("express");
var archiver = require("archiver");
var app = express();
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'password',
database : 'test'
});
app.get('/file', (req, res) => {
res.writeHead(200, {
'Content-Type': 'application/zip',
'Content-disposition': 'attachment; filename=archive-name.zip'
var zip = archiver('zip', {
zlib: { level: 9 }
});
zip.pipe(res);
zip.on('end', function() {
console.log('Archive wrote %d bytes', zip.pointer());
});
const queriesArray = ["SELECT * FROM tb1", "SELECT * FROM tb2"];
async.forEachOf(queriesList, (query) => {
connection.query(query, (err, results) => {
if(!err) {
zip.append(JSON.stringify(results), {name: `${query}.txt`})
}
else {
console.log("Error while performing Query");
}
})
}, function(err) {
if (err) {
console.log("error")
}
else {
zip.finalize();
}
})
})
const port = process.env.PORT || 7000;
app.listen(port, () => {
console.log('Listen on port ' + port);
})
这段代码应该创建一个包含多个文本文件的 zip 存档。访问 /file
页面的用户必须看到文件下载提示,但似乎 zip.finalize()
不工作所以我无法下载创建的存档(下载在我访问 [=11= 时开始] 但没有完成)。为什么我的代码不起作用?
这是因为您的回调函数从未被调用,因为 forEachOf
不知道它已完成所有异步任务。
iteratee 函数有一个最终参数,它是一个回调函数,您必须调用它才能知道特定的异步进程已完成,但您永远不会使用它。
https://caolan.github.io/async/global.html#AsyncFunction
An "async function" in the context of Async is an asynchronous
function with a variable number of parameters, with the final
parameter being a callback. (function (arg1, arg2, ..., callback) {})
The final callback is of the form callback(err, results...)
, which
must be called once the function is completed.
因此,要修复您的代码,请确保在查询调用的回调中调用迭代函数的回调方法
async.forEachOf(queriesList, (query,key,callback) => {
connection.query(query, (err, results) => {
if(!err) {
zip.append(JSON.stringify(results), {name: `${query}.txt`})
}
else {
console.log("Error while performing Query");
}
callback();
})
我正在尝试将 MySQL 查询的输出动态写入存档。这是我的代码:
var async = require("async");
var mysql = require("mysql");
var express = require("express");
var archiver = require("archiver");
var app = express();
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'password',
database : 'test'
});
app.get('/file', (req, res) => {
res.writeHead(200, {
'Content-Type': 'application/zip',
'Content-disposition': 'attachment; filename=archive-name.zip'
var zip = archiver('zip', {
zlib: { level: 9 }
});
zip.pipe(res);
zip.on('end', function() {
console.log('Archive wrote %d bytes', zip.pointer());
});
const queriesArray = ["SELECT * FROM tb1", "SELECT * FROM tb2"];
async.forEachOf(queriesList, (query) => {
connection.query(query, (err, results) => {
if(!err) {
zip.append(JSON.stringify(results), {name: `${query}.txt`})
}
else {
console.log("Error while performing Query");
}
})
}, function(err) {
if (err) {
console.log("error")
}
else {
zip.finalize();
}
})
})
const port = process.env.PORT || 7000;
app.listen(port, () => {
console.log('Listen on port ' + port);
})
这段代码应该创建一个包含多个文本文件的 zip 存档。访问 /file
页面的用户必须看到文件下载提示,但似乎 zip.finalize()
不工作所以我无法下载创建的存档(下载在我访问 [=11= 时开始] 但没有完成)。为什么我的代码不起作用?
这是因为您的回调函数从未被调用,因为 forEachOf
不知道它已完成所有异步任务。
iteratee 函数有一个最终参数,它是一个回调函数,您必须调用它才能知道特定的异步进程已完成,但您永远不会使用它。
https://caolan.github.io/async/global.html#AsyncFunction
An "async function" in the context of Async is an asynchronous function with a variable number of parameters, with the final parameter being a callback.
(function (arg1, arg2, ..., callback) {})
The final callback is of the formcallback(err, results...)
, which must be called once the function is completed.
因此,要修复您的代码,请确保在查询调用的回调中调用迭代函数的回调方法
async.forEachOf(queriesList, (query,key,callback) => {
connection.query(query, (err, results) => {
if(!err) {
zip.append(JSON.stringify(results), {name: `${query}.txt`})
}
else {
console.log("Error while performing Query");
}
callback();
})