I am a node beginner, and I am trying to configure the download of a multi-file image through a formidable one, which will have a progress bar, as well as a callback after each download that will dynamically load all the images on the same page after they are loaded. I understand that I need to use socket.io to interact with the browser after starting the download. Grozny has a listener of events for "progress." I need the client side of the script to get the bytes received to create the loading bar. After the file is downloaded, I want socket.io to pass the URL of the image upload location to the client so that the client can load the image and add it to the DOM dynamically. Here is the code I have.
My question is how to structure socket.io code inside an event listener for βprogressβ so that it can broadcast to the client?
Here is some free pseudo code to show you what I'm talking about:
//formidable code: app.post('/new', function(req,res){ var form = new formidable.IncomingForm(); form.addListener('progress', function(bytesReceived, bytesExpected){ //Socket.io interaction here?? }); form.uploadDir = __dirname + '/public/images/'; form.on('file', function(field, file) { //rename the incoming file to the file name fs.rename(file.path, form.uploadDir + "/" + file.name); console.log(form.uploadDir + "/" + file.name); }) }); //socket.io code io.sockets.on('connection', function (socket) { socket.on('upload', function (msg) { socket.broadcast.emit('progress', bytesReceived); }); });
source share