I am brand new to angular MEAN and I am currently attempting to upload a file, specifically a PDF, and save it to the server. Despite my efforts, I have been unable to locate any examples on how to actually save the uploaded file to the server's storage.
In this project, I am utilizing the ng-file-upload directive from https://github.com/danialfarid/ng-file-upload, Express for the server, and AngularJS for the file upload functionality.
POST UPDATED!! Please see below
Additional information: For this project, I am using Yeoman's full mean stack generator.
UPDATE: I attempted to use multer (https://github.com/expressjs/multer) to save the uploaded file to the server. However, when trying to upload the file, I encountered a 500 error with the following message:
Error: Unexpected field
at makeError ({proj_folder}/node_modules/multer/lib/make-error.js:12:13)
...
Updated HTML
<form accept-charset="UTF-8" class="form" name="form" ng-submit="$ctrl.submitForm(form)"
enctype="multipart/form-data">
...
<input ngf-select ng-model="$ctrl.paperFile" ngf-model-options="{allowInvalid: true}" name="paper" ngf-accept="'application/pdf'" required="" type="file" >
...
</form>
submitForm method
...
this.Upload.upload({
url:'/paperUpload',
method: 'POST',
file: this.paperFile,
fields:{
_id:this.user._id
}
})
.then(function(resp){
console.log('Success upload');
console.log(resp.data);
}, function(error){
console.log('fail upload');
console.log(error);
}, function(evt){
console.log('upload on progress');
console.log(evt);
});
Server route:
var express = require('express');
var multer = require('multer');
var router = express.Router();
var upload = multer({dest:'uploads/',
rename: function(fieldname, filename){
return filename+"_"+Date.now();
}});
router.post('/paperUpload', upload.single('paper'), uploadPaper);
...
//method to upload
export function uploadPaper(req,res){
res.status(204).end();
}
Although the 'uploads' folder is created, the file is not being uploaded and consistently results in failure.
Any assistance would be greatly appreciated. Thank you