0

I'm developer and I try to learn some of AngularJs, now i try to do a form, and is necessary send a file from input[file] to php, but when I click the buttom the file is not sended to this php, Try with some examples, but none worked.

Regards!!

my code:

<form>
    <input type="file" file-model="csvFile" name="contactFile" on-read-file="showContent($fileContent)" class="filestyle glyfecha glyphicon input-sm" />
    <button type="submit" class="btn btn-success btn-block" ng-click="sendForm()" style="margin-top: 10px;margin-bottom: 10px;">Guardar contactos</button>
</form>


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(){
                console.log(element[0].files[0]);     //this work
                modelSetter(scope, element[0].files[0]);
            });
        });
    }
};
}]);

app.service('fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function(file){
    var fd = new FormData();
    fd.append('file', file);
    $http.post('v1/contact/upload/0', fd, {
        transformRequest: angular.identity,
        headers: {'Content-Type': undefined}
    })
    .then(function(response){
        console.log(response.data);
    })
}
}]);

app.controller('addcontacto', ['$scope', 'fileUpload', function($scope, fileUpload){
$scope.sendForm = function() {
    var file = $scope.csvFile;
    fileUpload.uploadFileToUrl(file);
};
}]);
6
  • (1) Put a console.log('hello') inside your app.controller code. Does it appear when you load the web page? (2) Is there more this file you haven't shown? Eg, I don't see ng-app or ng-controller anywhere, I wanted to confirm if you omitted them from the example intentionally. Commented Dec 29, 2016 at 21:44
  • @therobinkim thank, but i can see the "hello" , I omitted intentionally the files ,ng-app and ng-controller Commented Dec 29, 2016 at 21:51
  • What is purpose of headers: {'Content-Type': undefined}? Commented Dec 29, 2016 at 21:53
  • @guest271314 I copy it directly from tutorials, in several I have seen that write it like this Commented Dec 29, 2016 at 21:56
  • Can you include php at Question? Commented Dec 29, 2016 at 21:59

0

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.