在我的項目中,我使用blueimp jQuery-File-Upload plugin一次將數GB的大文件一次上傳到服務器,一次大小爲10MB大塊。無法使用快速會話中存儲的引用寫入數據流
我有一個writeStream
在Node.js後端附加到express-session,只是這樣從2個不同的用戶上傳的文件不會搞砸。我爲每次上傳創建一個writeStream
並將其附加到會話。
問題是,,當在後端收到第一個數據塊時,在會話中找不到writeStream.write
。
我的Node.js/Express.js代碼有關部分:
// session config:
expressApp.use(session({
genid: function(req) {
return uuidV1();
},
secret: '***',
resave: true,
saveUninitialized: true,
cookie: { secure: false }
}));
// create writeStream
expressApp.post('/createStream', function(request, response) {
request.session.wstream = fileSystem.createWriteStream(uploadsDirectoryLocation + '/' + request.body.fileName);
request.session.wstream.on('finish', function() {
console.log('*********************************** file has been written ***********************************');
});
response.send({ response: 'success' });
});
// on every chunk received, write to the stream
expressApp.post('/uploadFile', function(request, response) {
var size = 0;
console.log(request.session.wstream); // the attached 'wstream' does show up
request.on('data', function(data) {
size += data.length;
request.session.wstream.write(data); // app crashes here
console.log('Got chunk: ' + data.length + ' total: ' + size);
console.log('data', data);
});
request.on('end', function() {
console.log("total size = " + size);
response.send("response");
});
request.on('error', function(e) {
console.log("ERROR: " + e.message);
response.send("response");
});
});
// close the stream
expressApp.post('/closeStream', function(request, response) {
request.session.wstream.end();
response.send({ response: 'success' });
});
確切的錯誤是:
streamObj.wstream.write(data);
^
TypeError: Cannot read property 'write' of undefined
什麼我錯在這裏做什麼?
編輯:
然而,如果我不重視的writeStream
到會話,並保持其全球性的,它的工作。但是,不適用於不同的會議。
我該如何解決這個問題?
爲什麼downvote? –