Note that there are some explanatory texts on larger screens.

plurals
  1. PONode.js AJAX file uploader
    text
    copied!<p>I am trying to make a Node.js file uploader with a AJAX progress bar.</p> <pre><code>var formidable = require('./formidable'), http = require('http'), sys = require('sys'); http.createServer(function(req, res) { if (req.url == '/upload' &amp;&amp; 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('&lt;?xml version="1.0"?&gt;&lt;response&gt;&lt;status&gt;1&lt;/status&gt;&lt;message&gt;'+ 000 +'&lt;/message&gt; &lt;/response&gt;'); res.end(); } // show a file upload form res.writeHead(200, {'content-type': 'text/html'}); res.end ( '&lt;form action="/upload" enctype="multipart/form-data" method="post"&gt;' + '&lt;p id="statuslabel"&gt;&lt;/p&gt;&lt;p id="anotherlabel"&gt;&lt;/p&gt;' + '&lt;input type="text" name="title" id="title"&gt;&lt;br&gt;' + '&lt;input type="file" name="upload" multiple="multiple"&gt;&lt;br&gt;' + '&lt;input type="submit" value="Upload" id="uploadform"&gt;' + '&lt;/form&gt;' ); }).listen(8000, '127.0.0.1'); </code></pre> <p>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.</p> <p>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 ? </p> <p>Thanks, Alex</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload