Bidirectional File Transfers For Node.js via Socket.IO - temporary publish, waiting on pull request
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'listen5001dl = require'delivery'fs = require'fs';iosocketson'connection'var delivery = dllistensocket;deliveryon'receive.success'fswriteFilefilenamefilebufferiferrconsole.log'File could not be saved.';elseconsole.log'File saved.';;;;;
$var socket = ioconnect'';socketon'connect'var delivery = socket;deliveryon'delivery.connect'$"input[type=submit]"clickvar file = $"input[type=file]"0files0;deliverysendfile;evtpreventDefault;;;deliveryon'send.success'console.log"file was successfully sent.";;;;
var io = require'socket.io'listen5001dl = require'delivery';iosocketson'connection'var delivery = dllistensocket;deliveryon'delivery.connect'deliverysendname: 'sample-image.jpg'path : './sample-image.jpg';deliveryon'send.success'console.log'File successfully sent to client!';;;;
$var socket = ioconnect'';socketon'connect'var delivery = socket;deliveryon'receive.start'console.log'receiving a file!';;deliveryon'receive.success'if fileisImage$'img'attr'src' filedataURL;;;;;
iosocketson'connection'var delivery = dllistensocket;deliveryon'receive.success'fswriteFilefilename filebufferiferrconsole.log'File could not be saved: ' + err;elseconsole.log'File ' + filename + " saved";;;;;
socketon 'connect'log "Sockets connected" ;delivery = dllisten socket ;deliveryconnect;deliveryon'delivery.connect'deliverysendname: 'sample-image.jpg'path : './sample-image.jpg';deliveryon'send.success'console.log'File sent successfully!';;;;
dl = require'delivery';
var delivery = dllistensocket;
deliverysendname: 'fileName.png'path: 'path/to/file/fileName.png';
deliverysendAsTextname: 'fileName.txt'path: 'path/to/file/fileName.txt';
delivery.connect is called when a client connects to the server.
receive.start is called when the server starts receiving a file. The callback function takes a filePackage object that describes the file being sent.
receive.success is called once the file has been successfully reveived by the server. The callback function takes a filePackage.
deliveryon'receive.success'fswriteFilefilenamefilebufferiferrconsole.log'File could not be saved.';elseconsole.log'File saved.';;;;
file.load is called after .send() is called and immediately after the file is loaded. The callback function takes a filePackage.
deliveryon'file.load'console.logfilePackagename + " 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.
deliveryon'send.start'console.logfilePackagename + " 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.
deliveryon'send.success'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
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.
returns true if the server used
returns the text representation of the file sent. If the file has been base64 encoded it returns the base64 encoded version of the file.
returns the base64 representation of the file prefixed with the data and mimetype necessary to display an image within
Client events mirror those on the server, see server events above for more details.
Delivery.js is released under the MIT license: