我不能设法用express从nodejs返回http响应中的zip文件。我使用的是ArchiverJS和azure-function-express。我已经试了一天了,但是我不能让它工作..
我的代码:
const createHandler = require("azure-function-express").createHandler;
const express = require("express");
// Create express app as usual
const app = express();
app.get("/api/download", (req, res) => {
var Archiver = require('archiver');
// Tell the browser that this is a zip file.
res.writeHead(200, {
'Content-Type': 'application/zip',
'Content-disposition': 'attachment; filename=myFile.zip'
});
var zip = Archiver('zip');
// Send the file to the page output.
zip.pipe(res); // <--- GENERATES TypeError: Cannot read property 'length' of null
// Create zip with some files. Two dynamic, one static. Put #2 in a sub folder.
zip.append('Some text to go in file 1.', { name: '1.txt' })
.append('Some text to go in file 2. I go in a folder!', { name: 'somefolder/2.txt' })
.file('staticFiles/3.txt', { name: '3.txt' })
.finalize();
});
// Binds the express app to an Azure Function handler
module.exports = createHandler(app);我没有任何管道方面的经验,但我认为这是它失败的地方。
这将产生以下错误,我打赌它与"zip.pipe(res)“有关。也许有人能解释一下我做错了什么?
[error] Worker 50fea52b-2a26-4eee-bf1a-5956a3bda16f uncaught exception (learn more: https://go.microsoft.com/fwlink/?linkid=2097909 ): TypeError: Cannot read property 'length' of null
at ServerResponse._send (_http_outgoing.js:231:33 undefined)
at write_ .write_ (_http_outgoing.js:625:15 undefined)
at ServerResponse.write (_http_outgoing.js:567:10 undefined)
at Archiver.ondata (C:\Users\KENUBBE\Desktop\smarthelmet-ml\node_modules\readable-stream\lib\_stream_readable.js:681:20 undefined)
at Archiver.emit (events.js:198:13 undefined)
at addChunk .addChunk (C:\Users\KENUBBE\Desktop\smarthelmet-ml\node_modules\readable-stream\lib\_stream_readable.js:298:12 undefined)
at readableAddChunk .readableAddChunk (C:\Users\KENUBBE\Desktop\smarthelmet-ml\node_modules\readable-stream\lib\_stream_readable.js:280:11 undefined)
at Archiver.Readable.push (C:\Users\KENUBBE\Desktop\smarthelmet-ml\node_modules\readable-stream\lib\_stream_readable.js:241:10 undefined)
at Archiver.Transform.push (C:\Users\KENUBBE\Desktop\smarthelmet-ml\node_modules\readable-stream\lib\_stream_transform.js:139:32 undefined)
at Archiver.afterTransform (C:\Users\KENUBBE\Desktop\smarthelmet-ml\node_modules\readable-stream\lib\_stream_transform.js:88:10 undefined)发布于 2020-05-01 00:11:29
我已经做了一个最小的工作示例,它与您的基本相同。这对我很管用。它使用:
节点@10.16.0
express@4.17.1
archiver@4.0.1
const express = require("express");
// Create express app as usual
const app = express();
const http = require('http');
const server = http.createServer(app);
app.get("/api/download", (req, res) => {
var Archiver = require('archiver');
// Tell the browser that this is a zip file.
res.writeHead(200, {
'Content-Type': 'application/zip',
'Content-disposition': 'attachment; filename=myFile.zip'
});
var zip = Archiver('zip');
// Send the file to the page output.
zip.pipe(res); // <--- GENERATES TypeError: Cannot read property 'length' of null
// Create zip with some files. Two dynamic, one static. Put #2 in a sub folder.
zip.append('Some text to go in file 1.', { name: '1.txt' })
.append('Some text to go in file 2. I go in a folder!', { name: 'somefolder/2.txt' })
.file('staticFiles/3.txt', { name: '3.txt' })
.finalize();
});
server.listen(7070);https://stackoverflow.com/questions/61511956
复制相似问题