Node.js AJAX Download File

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') {
    // 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 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

+5
source share
2 answers

, , - . , . → , "" , , .

, . , , .

, socket.io, , , .

+1

?

"progess":

Event: 'progress' (bytesReceived, bytesExpected)

.

, , , , .

0

All Articles