Nirbhay Tandon Nirbhay Tandon - 5 months ago 91
AngularJS Question

Clear Button wont work with Upload in AngularJs

I am new to AngularJs and am currently working on creating a file upload script.
I searched the web and combined a few scripts to achieve the code below.

My problem is that the clear button should clear the filename on click and remove the file from the queue.

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>

</head>

<body ng-app = "myApp">

<div ng-controller = "myCtrl">

<input type = "file" file-model = "myFile" file-select="file"/>

<button ng-click="clear()">clear</button>
<button ng-click = "uploadFile()">upload me</button>
</div>

<script>
var app = angular.module('myApp', []);

app.controller('myCtrl', ['$scope', 'fileUpload', 'fileSelect', function($scope, fileUpload, fileSelect){
$scope.uploadFile = function(){
var file = $scope.myFile;

console.log('file is ' );
console.dir(file);

var uploadUrl = "/fileUpload";
fileUpload.uploadFileToUrl(file, uploadUrl);
};

$scope.clear = function() {
$scope.file = null;
};
}]);

app.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]);
});
});
}

};
}]);

app.directive('fileSelect', function() {

return function( scope, elem, attrs ) {
var selector = $parse(attrs.fileSelect);
var modelSelector = elem.append(selector);

selector.bind('change', function( event ) {
scope.$apply(function() {
scope[ attrs.fileSelect ] = event.originalEvent.target.files;
});
});
scope.$watch(attrs.fileSelect, function(file) {
selector.val(file);
});
};
});

app.service('fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function(file, uploadUrl){
var fd = new FormData();
fd.append('file', file);

$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})

.success(function(){
})

.error(function(){
});
}
}]);



</script>

</body>
</html>

Answer

clear function should be defined as below

$scope.clear = function () {
    angular.element("input[type='file']").val(null);
};

Check the snippet below.

var app = angular.module('myApp', []);

	app.controller('myCtrl', ['$scope', function($scope){
		$scope.uploadFile = function(){
			var file = $scope.myFile;

			console.log('file is ' );
			console.dir(file);

			var uploadUrl = "/fileUpload";
			fileUpload.uploadFileToUrl(file, uploadUrl);
		};

		$scope.clear = function () {
			angular.element("input[type='file']").val(null);
		};

	}]);
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app = "myApp">

	<div ng-controller = "myCtrl">

		<input type = "file" ng-model = "myFile" file-select="file"/>

		<button ng-click="clear()">clear</button>
	</div>
  </body>