I am using Nodejs + Multer +angularjs for uploading files on the server.
i have a simple HTML file:
<form action="/multer" method="post" enctype="multipart/form-data">
<input type="file" id="photo" name="photo"/>
<button id="Button1">Upload</button>
</form>
Nodejs part:
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads/')
},
filename: function (req, file, cb) {
cb(null, file.originalname)
}
})
app.post('/multer', upload.single('photo'), function (req, res) {
res.end("File uploaded.");
});
this works perfectly and the file is successfully uploaded.
but this redirect me to "/multer" after uploading the file (because of the form element).
How do i stay on the same page??..possibly using angularjs
so i tried this:
making a HTML angular file:
<section data-ng-controller="myCtrl">
<input type="file" id="photo" name="photo"/>
<button id="Button1" ng-click="f()">Upload</button>
</section>
and a Angularjs controller:
angular.module('users').controller('myCtrl',[$scope,function($scope){
$scope.f=function(){
var photo = document.getElementById('photo');
var file = photo.files[0];
if (file) {
//code to make a post request with a file object for uploading?????
//something like..
//$http.post('/multer', file).success(function(response) {
//console.log("success");
//});
}
}
}]);
CAN SOMEONE HELP ME WITH THE CODE FOR MAKING A POST REQUEST WITH A FILE OBJECT FOR UPLOADING USING MULTER FROM ANGULARJS CONTROLLER ?
thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…