Hi! I am fairly new to using the MeanJS repo and I'm building off of it. I was using some code with multer that saves photos locally and loading the photos using the path provided. However, since then, I tried saving the photos to mongoose and I've been having some issues with that and I'm not sure how to do it.My client side controller is:var upload = function(file){ console.log("upload called"); var fd = new FormData(); fd.append('myfile', file.upload); return $http.post('/api/articles/', fd, { transformRequest: angular.identity, headers: { 'Content-Type': undefined } }); }; $scope.file = {}; $scope.uploadSubmit = function () { console.log("uploadsubmit called"); $scope.uploading = true; upload($scope.file).then(function (data) { if(data.data.success) { console.log("datadatasuccess"); $scope.uploading = false; $scope.alert = 'alert alert-success'; $scope.message = data.data.message; $scope.file = {}; } else { console.log("failed"); $scope.uploading = false; $scope.alert = 'alert alert-danger'; $scope.message = data.data.message; $scope.file = {}; } }); }; The server side model schema contains:thumbnail: { data: Buffer, contentType: String }, And the server side controller has:var storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, './modules/articles/client/img/'); // where to store it }, filename: function (req, file, cb) { if(!file.originalname.match(/\.(png|jpg|jpeg|pdf|gif)$/)) { console.log("filename error"); var err = new Error(); err.code = 'filetype'; // to check on file type return cb(err); } else { console.log("produced filename"); var day = new Date(); var d = day.getDay(); var h = day.getHours(); var fileNamee = d + '_' + h + '_' + file.originalname; console.log("filename produced is: " + fileNamee); cb(null, fileNamee); } } }); var upload = multer({ storage: storage, limits: { fileSize: 20971520 } // Max file size: 20MB }).single('myfile'); // name in form exports.uploads = function (req, res) { console.log("in uploads"); upload(req, res, function (err) { if (err) { if (err.code === 'LIMIT_FILE_SIZE') { res.json({ success: false, message: 'File size is too large. Max limit is 20MB' }); } else if (err.code === 'filetype') { res.json({ success: false, message: 'File type is invalid. Accepted types are .png/.jpg/.jpeg/.pdf' }); } else { console.log('err = ' + err); res.json({ success: false, message: 'File was not able to be uploaded' }); } } else { if (!req.file) { console.log("reached here with !req.file"); var article = new Article(req.body); article.user = req.user; //article.thumbnail.data = fs.readFileSync(req.files.userPhoto.path); article.thumbnail.contentType = "image/png"; console.log(article); article.save(function (err) { if (err) { return res.status(400).send({ message: errorHandler.getErrorMessage(err) }); } else { res.jsonp(article); } }); } else if (req.file) { console.log("reached here with req.file"); console.log(req.file); res.json({ success: true, message: 'File was uploaded!' }); } } // Everything went fine }); }; I noticed that exports.upload on the server side gets called twice, the first time without req.file and the second time, calling storage (with multer) and creating the filename and destination, and afterwards, jumping back to exports.upload with req.file. I don't know why its doing that, since I think that the client controller is only making one call (I'm most likely wrong about that). On top of that, I know that the line//article.thumbnail.data = fs.readFileSync(req.files.userPhoto.path); is wrong but ideally, I was looking to store the photo in the database as a Buffer, so it could be read into the article from there. I'm not sure how to find out what req is (when I print it out, its the [object Object] thing)
Submitted November 03, 2017 at 02:35PM by AndroidGuru7
No comments:
Post a Comment