
- AngularJS Tutorial
- AngularJS - Home
- AngularJS - Overview
- AngularJS - Environment Setup
- AngularJS - MVC Architecture
- AngularJS - First Application
- AngularJS - Directives
- AngularJS - Expressions
- AngularJS - Controllers
- AngularJS - Filters
- AngularJS - Tables
- AngularJS - HTML DOM
- AngularJS - Modules
- AngularJS - Forms
- AngularJS - Includes
- AngularJS - AJAX
- AngularJS - Views
- AngularJS - Scopes
- AngularJS - Services
- AngularJS - Dependency Injection
- AngularJS - Custom Directives
- AngularJS - Internationalization
- AngularJS Applications
- AngularJS - ToDo Application
- AngularJS - Notepad Application
- AngularJS - Bootstrap Application
- AngularJS - Login Application
- AngularJS - Upload File
- AngularJS - In-line Application
- AngularJS - Nav Menu
- AngularJS - Switch Menu
- AngularJS - Order Form
- AngularJS - Search Tab
- AngularJS - Drag Application
- AngularJS - Cart Application
- AngularJS - Translate Application
- AngularJS - Chart Application
- AngularJS - Maps Application
- AngularJS - Share Application
- AngularJS - Weather Application
- AngularJS - Timer Application
- AngularJS - Leaflet Application
- AngularJS - Lastfm Application
- AngularJS Useful Resources
- AngularJS - Questions and Answers
- AngularJS - Quick Guide
- AngularJS - Useful Resources
- AngularJS - Discussion
AngularJS - Upload File
We are providing an example of Upload File. To develop this app, we have used HTML, CSS and AngularJS. Following example shows about how to upload the file using AngularJS.
<html> <head> <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script> </head> <body ng-app = "myApp"> <div ng-controller = "myCtrl"> <input type = "file" file-model = "myFile"/> <button ng-click = "uploadFile()">upload me</button> </div> <script> var myApp = angular.module('myApp', []); myApp.directive('fileModel', ['$parse', function ($parse) { return { restrict: 'A', link: function(scope, element, attrs) { var model = $parse(attrs.fileModel); var modelSetter = model.assign; element.bind('change', function() { scope.$apply(function() { modelSetter(scope, element[0].files[0]); }); }); } }; }]); myApp.service('fileUpload', ['$https:', function ($https:) { this.uploadFileToUrl = function(file, uploadUrl) { var fd = new FormData(); fd.append('file', file); $https:.post(uploadUrl, fd, { transformRequest: angular.identity, headers: {'Content-Type': undefined} }) .success(function() { }) .error(function() { }); } }]); myApp.controller('myCtrl', ['$scope', 'fileUpload', function($scope, fileUpload) { $scope.uploadFile = function() { var file = $scope.myFile; console.log('file is ' ); console.dir(file); var uploadUrl = "/fileUpload"; fileUpload.uploadFileToUrl(file, uploadUrl); }; }]); </script> </body> </html>
Result
Open above saved code file in a web browser. See the result.
Advertisements
To Continue Learning Please Login