Node.js文件上传到Gridfs - 错误:请求已中止

时间:2013-11-22 17:02:11

标签: javascript node.js mongodb gridfs gridfs-stream

我通过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>

另外我想我应该提一下,在我收到上述错误后,文件再次开始上传但仍然失败。 同样,较小的文件也没问题。所以我的问题是如何使用这种方法有效地传输大文件,或者有更好的方法来做到这一点?感谢。

0 个答案:

没有答案