Hi,
I am trying to make a node.js file uploader with a 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') {
// 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');
The jquery is quite lengthy so I have cut it out but all it does is start a timer and request data from update and set on the label.
With this code will node accept multiple uploads from different hosts ? Also firefox doesnt seem to work but safari/chrome does any idea ? How would I request a status for the file upload ?
Thanks, Alex