如果我查看node-formidable文档,我可以阅读:
"Event: 'progress' (bytesReceived, bytesExpected)
Emitted after each incoming chunk of data that has been parsed.
Can be used to roll your own progress bar."
Run Code Online (Sandbox Code Playgroud)
我想知道如何实现我自己的进度条我的意思是如何阅读该信息客户端?我很困惑.它是通过在POST开始后启动的轮询GET实现的,还是可以在上传时从POST请求中读取信息?
如果我看看这个:
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(sys.inspect({fields: fields, files: files}));
});
return;
}
Run Code Online (Sandbox Code Playgroud)
看起来/ upload url正在处理POST请求并返回一些res.write('收到上传:\n \n');
我的问题是谁可以阅读
res.write('received upload:\n\n');
Run Code Online (Sandbox Code Playgroud)
你不需要覆盖onPart钩子!
只需在每个表单progress事件回调调用中通过套接字广播消息,如下所示:
form.on('progress', function(bytesReceived, bytesExpected) {
var progress = {
type: 'progress',
bytesReceived: bytesReceived,
bytesExpected: bytesExpected
};
socket.broadcast(JSON.stringify(progress));
});
Run Code Online (Sandbox Code Playgroud)
比在客户端上侦听套接字消息.
使用socket.io是一个不错的选择
记得覆盖onPart钩子。
incomingForm.onPart = function(part) {
part.addListener('data', function() {
// send back to the client the status
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8102 次 |
| 最近记录: |