0

I am trying to upload form with image file with angular $http function and using multer at background to receive. I know how to submit the form directly(without angular), I can successfully upload it via:

    <form method="post"  enctype="multipart/form-data">  
        <input type="file" name="avatar" /><br/>  
        <input type="submit" value="submit"/><br/>  
    </form>  

In this way, the image will be at req.file. However, when I try to use angular http to upload it:

    <form name="myForm" ng-model="formData" enctype="multipart/form-data"> 
        <input type="file" file-model="avatar" name="avatar" required/><br/>  
        <label class="item item-input">
            <button class="button button-block button-positive" ng-click="submit()" class="btn btn-primary btn-block">submit</button>
        </label>
        <label class="item item-input">
            <button class="button button-block button-dark" ng-click="home()" >back</button>
        </label>
    </form>

js:

$scope.submit = function() {
        var fd = new FormData();
        fd.append('avatar', $scope.avatar);
        $http({
            url:'/avatar',
            method: 'POST',            
            data: fd,
            headers: {
                'Content-Type': 'undefined'
            },
            transformRequest: angular.identity    
        }).success(function(data,status){
            if(status == 200) {
                window.location = '/home';
            }

        }).error(function(status, data){
            window.location = '/avatar';
        })
    }

the req.file field become undefined. So how to upload my image via angular $http?

1 Answer 1

0

The "file-model"-directive is not a part of angular. Did you include the dependency? https://github.com/ghostbar/angular-file-model

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

Comments

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.