0

I would like to know how can I upload files using angularjs without using external library and just a single upload button? I have searched for the solution for a while and found a solution is pretty close to what I want as below:

<div ng-controller = "myCtrl">
    <input type="file" file-model="myFile"/>
    <button ng-click="uploadFile()">upload me</button>
</div>

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', ['$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(){
        });
    }
}]);

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);
    };

}]);

http://jsfiddle.net/JeJenny/ZG9re/

However, I want only a upload button and in the above case, how can I trigger uploadFile()?

1 Answer 1

1

Updated your fiddle: http://jsfiddle.net/ZG9re/7080/

HTML

<div ng-controller = "myCtrl">    
    <button>
    <input type="file" class="input" id="myfile" file-model="myFile"><a href="">Upload Me</a>
    </button>    
</div>

CSS

#myfile {
  opacity: 0;
  position: absolute;
}

So that now when you click on "Upload Me" you will get the file explorer.

Update

Please check: http://jsfiddle.net/ZG9re/7081/

Sign up to request clarification or add additional context in comments.

18 Comments

I am new to angularjs. I would like to know what the below two lines mean? Thanks! document.getElementById("test").classList.remove("open"); <!-- scope.uploadFile(); -->
I am sorry here is the updated code: jsfiddle.net/ZG9re/7081 I added that as I was testing something, you don't need that to run your code, you can remove that piece of code
So you don't need any change in your JavaScript, just the HTML and CSS needs to change
Yes you can, as I said there is no change in your JavaScript, just the HTML and CSS need to change as posted in answer :) like this: jsfiddle.net/ZG9re/7082
That you have to trigger in link function when the binding has some change, like this: jsfiddle.net/ZG9re/7083
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.