Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am playing around with AngularJS and have not been able to solve this problem. I have a view that has a form to upload a file to a node server. So far I have manage to do this using some directives and a service. I allow the user to send a custom name to the POST data if they desire. What I wan to accomplish is that when the user selects a file the filename models auto populates.

My view looks like:

<div>
    <input file-model="phpFile" type="file">
    <input name="filename" type="text" ng-model="filename">
    <button ng-click="send()">send</button>
</div>

file-model is my directive that allows the file to be assigned to a scope.

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

The service:

myApp.service('fileUpload', ['$http', function($http){
    this.uploadFileToUrl = function(file, uploadUrl, optionals) {
        var fd = new FormData();
        fd.append('file', file);
        for (var key in file) {
            fd.append(key, file[key]);
        }
        for(var i = 0; i < optionals.length; i++){
            fd.append(optionals[i].name, optionals[i].data);
        }
    });
}]);

Here as you can see I pass the file, append its properties, and append any optional properties.

In the controller is where I am having the troubles. I have tried $watch and using the file-model but I get the same error either way.

myApp.controller('AddCtrl', function($scope, $location, PEberry, fileUpload){
    //$scope.$watch(function() {
    //    return $scope.phpFile;
    //},function(newValue, oldValue) {
    //    $scope.filename = $scope.phpFile.name;
    //}, true);
    // if ($scope.phpFiles) {
    //     $scope.filename = $scope.phpFiles.name;
    // }
    $scope.send = function() {
        var uploadUrl = "/files";
        var file = $scope.phpFile;
        //var opts = [{ name: "uname", data: file.name }]

        fileUpload.uploadFileToUrl(file, uploadUrl);
    };
});

Thank you for your help!

share|improve this question

2 Answers 2

This plunker does the basic of what you've asked:

http://plnkr.co/edit/xbguJXKs4NShTuuIXrF7?p=preview

The main thing to note here is we use jquery to listen when selected file changes. I leave the watch of the filename on the scope and the rest of the logic to you.

share|improve this answer

I had a similar issue on a project. I solved it by using this library.

My input in my HTML file was this:

<input type="file" ng-file-select="onImageFileSelect($files, $event);" accept="image/*" >

In my controller, I had a function (some parts of that function were redacted):

$scope.onImageFileSelect = function($files, event) {
    $scope.userInfo.image.file = $files[0];
}

That set up the scope so that in my HTML file I could print the file name using:

<div>{{userInfo.image.file.name}}</div>

Since the user was uploading a profile image for their account, I wanted to give them the ability to automatically see a thumbnail. For that I used another library called fileReader that wraps the Javascript file reader function.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.