[英]file upload in angularjs
我是angularjs的新手,並嘗試實現簡單的文件上傳,但是嘗試從控制器訪問文件時我感到文件未定義
article.html
<section data-ng-controller="ArticlesController" data-ng-init="find()">
<div class="page-header">
</div>
<div >
<input type="file" file-model="myFile"/>
<button ng-click="uploadFile()">upload me</button>
</div>
</section>
以下是我試圖訪問文件對象的控制器
angular.module('articles').controller('ArticlesController', ['$scope', '$stateParams', '$location','Authentication', 'Articles',
function($scope, $stateParams, $location, Authentication, Articles) {
$scope.authentication = Authentication;
$scope.uploadFile = function() {
console.log('came here');
var file = $scope.myFile;
console.log('file is ' + file);
};
}
]);
編輯:
我嘗試使用https://github.com/ghostbar/angular-file-model並處理Meanjs樣板代碼
步驟之一是在html頁面中包含angular-file-model.js。 添加到您的HTML文件:
我要包含在哪個文件中,以及如何包含angular-file-model.js文件。
看來您正在使用Angular-File-Model
他們也有Plunker ,並且工作正常。 您是否遵循了git中提到的所有步驟? 檢查是否在主模塊中注入了文件模型依賴項,例如:
angular.module('articles', ['file-model']);
我已經多次使用Danial Farid的Angular File Upload,並取得了巨大的成功。
angular.module('myApp').directive('fileModel', ['$window',function ($window) {
return {
restrict: 'A',
require:'ngModel',
link: function(scope, element, attrs,ngModel) {
element.bind('change', function(){
scope.$apply(function(){
var r = new FileReader();
r.onloadend = function(){
var svg = r.result;
ngModel.$setViewValue($window.btoa(svg));
ngModel.$render();
};
var file = element[0].files[0];
if(file !== undefined){
if(file.type === 'image/svg+xml' || file.type === 'image/svg'){
ngModel.$setValidity('svgimage', true);
r.readAsDataURL(file);
}else{
ngModel.$setValidity('svgimage', false);
ngModel.$setViewValue(' ');
ngModel.$render();
}
var value = element[0].value ;
value = value.substr(value.lastIndexOf('\\') + 1,value.length);
document.getElementById('fileurl').value = value;
document.getElementById('fileurl').title = value;
}
});
});
}
};
}]);
<input ng-model="icon.icon" file-model class="form-control upload" type="file" required/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.