我正在使用Request上传文件。
req = request.post url: "http://foo.com", body: fileAsBuffer, (err, res, body) ->
console.log "Uploaded!"我如何知道实际上传了多少数据?是否有我可以订阅的事件,或者是否有我可以轮询的request属性?
如果没有,那么上传数据并知道已经上传了多少数据的最佳方法是什么?
发布于 2012-08-24 21:18:23
有人已经创建了一个很好的模块来实现这一点,该模块已经在transloadit的生产堆栈中运行(因此它是可靠的,并且维护良好)。你可以在这里找到它:
https://github.com/felixge/node-formidable
代码应如下所示:
var formidable = require('formidable'),
http = require('http'),
util = require('util');
http.createServer(function(req, res) {
if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
// parse a file upload
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
return;
}
// show a file upload form
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/upload" enctype="multipart/form-data" method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload" multiple="multiple"><br>'+
'<input type="submit" value="Upload">'+
'</form>'
);
}).listen(80);然后,您可以使用Socket.io将状态推送到客户端
有趣的注解:这是导致Node创建的问题之一。在node中,Ryan谈到了Node是如何开始尝试找到最佳的方式来实时通知用户关于web...anyway I分支上的文件上传状态的,但如果你对Node的历史感兴趣,这段视频值得一看
https://stackoverflow.com/questions/12098713
复制相似问题