Sending files to the server, and pushing files to the client should be as easy as possible. Delivery.js uses Node.js and Socket.IO to make it easy to push files to the client, or send them to the server. Files can be pushed to the client as text (utf8) or base64 (for images and binary files).
npm install delivery -g
delivery.js can be found within lib/client.
var io = require('socket.io').listen(5001),
dl = require('delivery'),
fs = require('fs');
io.sockets.on('connection', function(socket){
var delivery = dl.listen(socket);
delivery.on('receive.success',function(file){
var params = file.params;
fs.writeFile(file.name,file.buffer, function(err){
if(err){
console.log('File could not be saved.');
}else{
console.log('File saved.');
};
});
});
});
$(function(){
var socket = io.connect('http://0.0.0.0:5001');
socket.on('connect', function(){
var delivery = new Delivery(socket);
delivery.on('delivery.connect',function(delivery){
$("input[type=submit]").click(function(evt){
var file = $("input[type=file]")[0].files[0];
var extraParams = {foo: 'bar'};
delivery.send(file, extraParams);
evt.preventDefault();
});
});
delivery.on('send.success',function(fileUID){
console.log("file was successfully sent.");
});
});
});
var io = require('socket.io').listen(5001),
dl = require('delivery');
io.sockets.on('connection', function(socket){
var delivery = dl.listen(socket);
delivery.on('delivery.connect',function(delivery){
delivery.send({
name: 'sample-image.jpg',
path : './sample-image.jpg',
params: {foo: 'bar'}
});
delivery.on('send.success',function(file){
console.log('File successfully sent to client!');
});
});
});
$(function(){
var socket = io.connect('http://0.0.0.0:5001');
socket.on('connect', function(){
var delivery = new Delivery(socket);
delivery.on('receive.start',function(fileUID){
console.log('receiving a file!');
});
delivery.on('receive.success',function(file){
var params = file.params;
if (file.isImage()) {
$('img').attr('src', file.dataURL());
};
});
});
});
io.sockets.on('connection', function(socket){
var delivery = dl.listen(socket);
delivery.on('receive.success',function(file){
fs.writeFile(file.name, file.buffer, function(err){
if(err){
console.log('File could not be saved: ' + err);
}else{
console.log('File ' + file.name + " saved");
};
});
});
});
socket.on( 'connect', function() {
log( "Sockets connected" );
delivery = dl.listen( socket );
delivery.connect();
delivery.on('delivery.connect',function(delivery){
delivery.send({
name: 'sample-image.jpg',
path : './sample-image.jpg'
});
delivery.on('send.success',function(file){
console.log('File sent successfully!');
});
});
});
dl = require('delivery');
var delivery = dl.listen(socket);
delivery.on('delivery.connect',function(delivery){
...
});
delivery.send({
name: 'fileName.png',
path: 'path/to/file/fileName.png'
});
delivery.sendAsText({
name: 'fileName.txt',
path: 'path/to/file/fileName.txt'
});
delivery.connect is called when a client connects to the server.
delivery.on('delivery.connect',function(delivery){
...
});
receive.start is called when the server starts receiving a file. The callback function takes a filePackage object that describes the file being sent.
delivery.on('receive.start',function(filePackage){
console.log(filePackage.name);
});
receive.success is called once the file has been successfully reveived by the server. The callback function takes a filePackage.
delivery.on('receive.success',function(file){
fs.writeFile(file.name,file.buffer, function(err){
if(err){
console.log('File could not be saved.');
}else{
console.log('File saved.');
};
});
});
file.load is called after .send() is called and immediately after the file is loaded. The callback function takes a filePackage.
delivery.on('file.load',function(filePackage){
console.log(filePackage.name + " has just been loaded.");
});
send.start is called after .send() is called and immediately after the file begins being sent to the client. The callback function takes a filePackage.
delivery.on('send.start',function(filePackage){
console.log(filePackage.name + " is being sent to the client.");
});
send.success is called after .send() is called and once confirmation is received form the client that the the file sent was successfully received. The callback function takes the uid of the file that was sent.
delivery.on('send.success',function(uid){
console.log("File successfully sent!");
});
FilePackage objects encapsulate files and includes a text representation (utf8), or base64 representation of the file. They also include the file's meta data, including name
, size
and mimeType
.
filePackage.isImage()
returns true if the file has a corresponding mime type that is an image. It is possible that this method could return false if your file is an image, but does not have a mimetype, or does not have a mimetype of image/gif, image/jpeg, image/png, image/svg+xml, image/tiff. Look for var imageFilter
within delivery.js if you'd like to add additional mimetypes.
filePackage.isText()
returns true if the server used sendAsText()
.
filePackage.text()
returns the text representation of the file sent. If the file has been base64 encoded it returns the base64 encoded version of the file.
filePackage.dataURL()
returns the base64 representation of the file prefixed with the data and mimetype necessary to display an image within <img src=''>
.
<script src="/js/delivery.js"></script>
Client events mirror those on the server, see server events above for more details.
##License
Delivery.js is released under the MIT license:
http://www.opensource.org/licenses/MIT
- Incorporating feedback from other developers!
- Breaking files into pieces to help transfer larger files.
- md5 hash file and confirm the hash when a file has been received.
- ?