Guest User

Handling image data in express

a guest
Oct 24th, 2013
436
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function imageService(req,res) {
  2.  
  3. var mongoose = require('mongoose');
  4. var ImageAsset = require('../models/imageModel.js');=
  5. var path = require('path');
  6. var fs = require('fs');
  7. var uuid = require('node-uuid');
  8.  
  9. var uploadedImage = req.files.imageField;
  10.  
  11. var tempPath = uploadedImage.path;
  12. fs.readFile(tempPath, function (err, imageData) {
  13. var imageName = uploadedImage.name;
  14. if (!imageName) console.log("There was an error");
  15. else {
  16.  
  17. var newPath =uuid.v1(); //Save it with a newName
  18. var contentType = uploadedImage.headers["content-type"];
  19. fs.writeFile(newPath, uploadedImage, function (err) {
  20. if (err) console.log("Unable to writeFile:" + err);
  21. newImageData = {
  22. filePath: newPath,
  23. mimeType: contentType,,
  24. assetType: imageAssetType
  25. };
  26. //Save the image information in some database
  27. // imageModel contains schema model for the collection
  28.  
  29. //open db connection and handle accordingly
  30. var newImageAsset = new ImageAsset(newImageData);
  31. newImageAsset.save(function (err) {
  32. if (err) console.log(err)
  33. }
  34. }
  35. });
  36. }
RAW Paste Data