-
-
Save mygoare/955f764f2ab633143c854df571c81f47 to your computer and use it in GitHub Desktop.
busboy upload file, socketio update the progress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Busboy = require('busboy'); | |
var AWS = require('aws-sdk'); | |
var socket = require('socket.io'); | |
var express = require('express'); | |
var http = require('http'); | |
// Set up Express | |
var app = express(); | |
var server = http | |
.Server(app) | |
.listen(80); | |
var io = socket(server); | |
// keeps track of all the open sockets | |
var sockets = {}; | |
io.on('connection', function (socket) { | |
var uniqueId = _.random(0, 100000000); | |
app.socket[uniqueId] = socket; | |
socket.emit('id', uniqueId); | |
}); | |
// Set up upload route | |
app.post('/upload', function (req, res) { | |
AWS.config.update({ | |
appKey: '', | |
jsKey: '', | |
}); | |
var s3 = new AWS.S3(); | |
var busboy = new Busboy({ | |
headers: req.headers | |
}); | |
busboy.on('file', function(fieldname, file, filename) { | |
s3.upload({ | |
Bucket: 'bucketname', | |
Key: new Date().getTime() + filename, | |
Body: file //stream | |
}, function(err, file){ | |
res.json({ | |
success: true | |
}); | |
}).on('httpUploadProgress', function(evt) { | |
//emit progress | |
sockets[req.query.socketId].emit('uploadProgress', evt); | |
}); | |
}); | |
req.pipe(busboy); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('blueimp-file-upload'); | |
var socket = require('socket.io-client'); | |
var fileSize; | |
var socketId; | |
// Socket will be used to transmit the percent uploaded from server | |
socket.connect() | |
// when the socket gives us a unique ID we will save it so that we | |
// can identify the client | |
.on('id', function (data) { | |
socketId = data; | |
}) | |
.on('uploadProgress', function (data){ | |
// you can show a progress bar with this percentage | |
var pct = data.loaded / fileSize; | |
}); | |
$('.file') | |
.change(function (){ | |
// save the file size so that we can calculate perfectage | |
fileSize = this.files[0].size; | |
}) | |
.fileupload({ | |
dataType: 'json', | |
}) | |
.fileupload('option', { | |
url: '/upload?socketId=' + socketId | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input type="file" class="file" name="files[]"> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment