Last active
February 17, 2016 20:36
-
-
Save amogower/669ad5d92dc1facb2b9b to your computer and use it in GitHub Desktop.
Multer File Upload - you need to npm install and save multer and s3 (Amazon s3) - app.js is the front-end app.js, not the api app.js
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
| function submitForm(){ | |
| event.preventDefault(); | |
| var $form = $(this) | |
| var method = $form.attr('method'); | |
| var url = "http://localhost:3000" + $form.attr('action'); | |
| var isFileUpload = $form.attr("enctype") === 'multipart/form-data'; | |
| var data = $form.serialize(); | |
| if(isFileUpload) { | |
| data = new FormData($form[0]); | |
| } | |
| if($(this).attr('action') === '/signup') | |
| $('.login-form').hide(); | |
| $('#homes, .home-page-jumbo').show(); | |
| return ajaxRequest(method, url, data, authenticationSuccessful, isFileUpload); | |
| } | |
| function ajaxRequest(method, url, data, callback, isFileUpload) { | |
| var options = { | |
| method: method, | |
| url: url, | |
| data: data, | |
| beforeSend: setRequestHeader | |
| }; | |
| if(isFileUpload) { | |
| options.cache = false; | |
| options.enctype = 'multipart/form-data'; | |
| options.processData = false; | |
| options.contentType = false; | |
| } | |
| return $.ajax(options) | |
| .done(function(data){ | |
| callback(data); | |
| }) | |
| .fail(function(data){ | |
| console.log(data.responseJSON.message); | |
| }); | |
| } |
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
| function createHome(req, res) { | |
| console.log(req.file, req.body); | |
| User.findById(req.user._id, function(err, user){ | |
| if(err) return res.render({messsage: 'Could not ceate home because ' + err}); | |
| user.local.home = { | |
| type: req.body.type, | |
| home_image: req.file.key, | |
| description: req.body.description, | |
| address: req.body.address, | |
| postcode: req.body.postcode, | |
| pets: [req.body.pets], | |
| date_from: req.body.date_from, | |
| date_to: req.body.date_to | |
| }; | |
| user.save(function(err){ | |
| if(err) return res.status(500).json({message: 'Could not add home to user because ' + err}); | |
| return res.status(200).json({ home: user.local.home }); | |
| }); | |
| }); | |
| }; |
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
| var multer = require('multer'); | |
| var s3 = require('multer-s3'); | |
| var upload = multer({ | |
| storage: s3({ | |
| dirname: 'homes/photos', | |
| bucket: process.env.AWS_BUCKET_NAME, | |
| secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, | |
| accessKeyId: process.env.AWS_ACCESS_KEY_ID, | |
| region: 'eu-west-1', | |
| filename: function (req, file, cb) { | |
| cb(null, file.originalname) | |
| } | |
| }) | |
| }); | |
| router.route('/api/homes') | |
| .get(homesController.getHomes) | |
| .post(upload.single('image'), homesController.createHome) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment