我通过 gridfs-stream 节点模块将大文件上传到 Gridfs 时遇到问题。我尝试上传较小的文件,并且运行良好,但是当我尝试上传较大的文件(~2gb 及更多)时,node.js 服务器给了我这个错误:
Error: Request aborted
at IncomingMessage.onReqAborted (D:\ProjectName\node_modules\express\node_modules\connect\node_modules\multiparty\index.js:131:17)
at IncomingMessage.EventEmitter.emit (events.js:92:17)
at abortIncoming (http.js:1911:11)
at Socket.serverSocketCloseListener (http.js:1923:5)
at Socket.EventEmitter.emit (events.js:117:20)
at TCP.close (net.js:466:12)
我的代码如下,这也是在本地节点服务器上完成的:
//crud.js file
var config = require('./config.js')
var fs = require('fs')
var Grid = require('gridfs-stream');
var gfs = Grid(config.db, config.mongo);
exports.uploadFile = function (req, res) {
var tempfile = req.files.filename.path;
var origname = req.files.filename.name;
var writestream = gfs.createWriteStream({ filename: origname });
fs.createReadStream(tempfile)
.on('end', function () {
res.send('<p><h1>Upload Complete!</h1><p>');
}).on('error', function () {
res.send('Error, upload failed!');
}).pipe(writestream);
}
还有我的主 server.js 文件:
app.configure(function () {
app.use(express.compress());
app.use('/', express.static(__dirname + '/upload'));
app.set('view engine', 'html');
app.use(express.logger('dev'));
app.use(express.bodyParser({limit: '1000000mb'}));
app.use(express.methodOverride());
app.use(app.router);
});
app.post('/', crud.uploadFile)
最后是我的客户:
<div class="col-lg-4">
<form id="form" method="post" action="" enctype="multipart/form-data">
<input type="file" id="file" name="filename" class="btn btn-warning">
<br>
<input type="submit" value="Upload file" class="btn btn-primary">
</form>
</div>
另外我想我应该提到,在我收到上述错误后,文件开始再次上传但仍然失败。同样,较小的文件也没有问题。所以我的问题是我如何能够使用这种方法有效地流式传输大文件,还是有更好的方法来做到这一点?谢谢。