Last active
August 29, 2015 14:07
-
-
Save lakenen/b3f42e733c2d15f8024a to your computer and use it in GitHub Desktop.
upload multipart file to view api
This file contains hidden or 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
/* | |
To run: | |
Get a Box View API token (https://box-view.readme.io/) and set it to `TOKEN` below. | |
npm install box-view busboy | |
node upload.js | |
navigate to http://localhost:8888 in your browser and submit a file | |
*/ | |
var TOKEN = 'your view api token'; | |
var http = require('http'), | |
Busboy = require('busboy'), | |
client = require('box-view').createClient(TOKEN); | |
var html = '<form method="POST" enctype="multipart/form-data">' + | |
'<input type="file" name="file"/>' + | |
'<input type="submit" value="go" /> ' + | |
'</form>'; | |
var server = http.createServer(function (req, res) { | |
res.writeHead(200, {'content-type': 'text/html'}); | |
if (req.method === 'POST') { | |
res.write('<pre>') | |
var busboy = new Busboy({ headers: req.headers }); | |
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { | |
res.write('uploading document...\n') | |
client.documents.uploadFile(file, function (err, doc) { | |
if (err) { | |
res.end('the document failed to upload... \n' + JSON.stringify(doc, true, 2)); | |
return; | |
} | |
res.write('upload successful. attempting to create a session...\n'); | |
client.sessions.create(doc.id, { retry: true }, function (err, sess) { | |
if (err) { | |
res.end('failed to create a session... \n' + JSON.stringify(sess, true, 2)); | |
return; | |
} | |
res.end('session created: <a href="' + sess.urls.view + '">view</a>'); | |
}); | |
}); | |
}); | |
req.pipe(busboy); | |
} else { | |
res.end(html) | |
} | |
}).listen(8888); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment