我想用AJAX進度條製作一個Node.js文件上傳器。Node.js AJAX文件上傳器
var formidable = require('./formidable'), http = require('http'), sys = require('sys');
http.createServer(function(req, res) {
if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
// parse a file upload
var form = new formidable.IncomingForm();
form.uploadDir = './node_uploads';
form.keepExtensions = true;
//print the upload status in bytes
form.addListener("progress", function(bytesReceived, bytesExpected) {
//progress as percentage
progress = (bytesReceived/bytesExpected * 100).toFixed(2);
mb = (bytesExpected/1024/1024).toFixed(1);
sys.print("Uploading "+mb+"mb ("+progress+"%)\015");
});
//enter here after upload complete
form.parse(req, function(fields, files) {
sys.debug("Upload Complete");
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end());
});
return;
}
if (req.url == '/update') {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('<?xml version="1.0"?><response><status>1</status><message>'+ 000 +'</message> </response>');
res.end();
}
// show a file upload form
res.writeHead(200, {'content-type': 'text/html'});
res.end
('<form action="/upload" enctype="multipart/form-data" method="post">'
+ '<p id="statuslabel"></p><p id="anotherlabel"></p>'
+ '<input type="text" name="title" id="title"><br>'
+ '<input type="file" name="upload" multiple="multiple"><br>'
+ '<input type="submit" value="Upload" id="uploadform">'
+ '</form>'
);
}).listen(8000, '127.0.0.1');
的jQuery是相當長,所以我都剪下來,但它所做的是從更新啓動一個定時器和請求數據,並在標籤上設置。
有了這段代碼,節點會接受來自不同主機的多次上傳嗎? 另外,Firefox似乎不起作用,但Safari/Chrome有什麼想法嗎? 我將如何請求文件上傳的狀態?
謝謝,亞歷克斯
我無法回答這個問題,但一定要停下[#node.js](http://webchat.freenode.net/?channels=node.js&uio=d4)並提出問題! – DTrejo 2011-03-30 04:46:03
你解決了嗎? – 2011-05-25 13:16:16
可悲的是,沒有空閒時間..這可能有助於https://github.com/felixge/node-formidable或檢查felixge的可調試博客文章,他涵蓋了這些問題的很多。 – Alex 2011-05-26 13:13:05