by
0
354 Views
3 Badges
Top 1% !
Popular
Specified
Popularity: 781st place
Created
Modified Jun 25, 2013

Published on:

No tags for this snippet yet.
LanguageJavaScript
SourceGithub

NodeJS Photo Upload with Azure Storage

NodeJS Photo Upload with Azure Storage: 
AzureStorage.js
post this code
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="http://www.snip2code.com/Embed/8974/NodeJS-Photo-Upload-with-Azure-Storage?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
exports.addCarPhoto = function(req, res) { var userID = req.user._id; var carsBaseURI = "http://sobrioapp.blob.core.windows.net/cars/"; // setup photo meta data var type = req.files.photo.type; var filename = req.params.id + "-" + req.files.photo.name; var path = req.files.photo.path; // check if it has the right filetype if(type == "image/jpeg" || type == "image/jpg" || type == "image/png") { // initiate a blobService object for Azure var blobService = azure.createBlobService(); var options = { contentType: type, metadata: { fileName: filename } } // Fire off in series so that the tmp photo isn't deleted before its fully uploaded async.series({ block: function(callback) { blobService.createBlockBlobFromFile('cars', filename, path, options, callback); }, unlink: function(callback) { fs.unlink(path, callback); } }, function(err, results) { // Update the user's car with the new photo var update = User.findOneAndUpdate({ "_id": userID, "cars._id": req.params.id }, { $set: { 'cars.$.photo': carsBaseURI + results.block[0].blob } }); var promise = update.exec(); promise.addBack(function(err, user) { if(err) { res.send(err); } else { res.send(user.cars); } }); }); } }
If you want to be updated about similar snippets, Sign in and follow our Channels
Originally posted on https://gist.github.com/5855940