我使用Nodejs + Multer + angularjs上傳服務器上的文件。
我有一個簡單的HTML文件:nodejs + multer + angularjs上傳,無需重定向
<form action="/multer" method="post" enctype="multipart/form-data">
<input type="file" id="photo" name="photo"/>
<button id="Button1">Upload</button>
</form>
部分的NodeJS:
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.");
});
這工作完全,文件上傳成功。
但是這個重定向到上傳文件後(因爲表單元素)「/ multer」。
我如何留在同一頁面? ..possibly使用angularjs
所以我嘗試這樣:
製備HTML文件角:
<section data-ng-controller="myCtrl">
<input type="file" id="photo" name="photo"/>
<button id="Button1" ng-click="f()">Upload</button>
</section>
和Angularjs控制器:
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");
//});
}
}
}]);
有人可以幫我使用角度控制器進行上傳的文件對象上傳文件的代碼?
感謝
您是否找到任何解決方案? –
@SimranKaur是的,我發現了一個解決方案:) –
@SimranKaur我已經發布了答案 –