I am trying to make a Node.js download file with an AJAX progress bar.
var formidable = require('./formidable'), http = require('http'), sys = require('sys');
http.createServer(function(req, res) {
if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
var form = new formidable.IncomingForm();
form.uploadDir = './node_uploads';
form.keepExtensions = true;
form.addListener("progress", function(bytesReceived, bytesExpected) {
progress = (bytesReceived / bytesExpected * 100).toFixed(2);
mb = (bytesExpected / 1024 / 1024).toFixed(1);
sys.print("Uploading "+mb+"mb ("+progress+"%)\015");
});
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();
}
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 is quite long, so I turned it off, but all it does is start a timer and request data from the update and install on it.
With this code, will node accept multiple downloads from different hosts? Also, Firefox doesn't seem to work, but does Safari / Chrome make any ideas? How can I request status for file upload?
Thanks, Alex
source
share