0

I am doing a file upload in angularjs. I am unable to get the file object content before sending to my back-end. XHR, headers:

XHR-Header

I have no idea why because I can get the content in my logs. console.log:

logs

These are my codes: html:

<body ng-app="myApp" ng-controller="appCtrl">
     <input type="file" id="file" name="files" accept="text/*" 
            data-url="file" class="inputfile_upload" select-ng-files
            ng-model="uploadedFile"/>
     <label for="file"> <span id="chooseFile"></span>Upload a file!</label>
     <button id="submitBtn" type="submit" ng-click="upload()">Upload</button>
</body>

controller.js:

var app = angular.module('myApp', [])
app.controller('appCtrl', function ($scope, $rootScope, $http, fileUploadService){

    $scope.upload = function() {
       var file = $scope.uploadedFile; 
       console.log('file is ' );
       console.dir(file);

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

service.js:

app.factory('fileUploadService', function ($rootScope, $http) {
    var service = {};
    service.uploadFileToUrl = function upload(file, uploadUrl){
        var fd = new FormData();
        fd.append('file', file);
        $http.post(uploadUrl, fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        }).then(function successCallback(response){
            console.log("Files added");
        }, function errorCallback(response){
            console.log("Files not successfully added");
        })    
    }
    return service;
});

directive.js:

app.directive("selectNgFiles", function($parse) {
  return {
    require: "ngModel",
    link: function postLink(scope,elem,attrs,ngModel) {
      elem.on("change", function(e) {
        var files = elem[0].files;
        ngModel.$setViewValue(files);
      })
    }
  }
});
georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • This looks like the same code as [this question](https://stackoverflow.com/questions/59489233/angularjs-unable-to-display-upload-file-logs/59490864#59490864) by [@user3774763](https://stackoverflow.com/users/3774763/user3774763). Are you using two accounts to post questions? – georgeawg Jan 03 '20 at 07:34
  • You are using my `select-ng-files` directive from [my answer](https://stackoverflow.com/a/43074638/5535245) in [ng-model for `` (with directive DEMO)](https://stackoverflow.com/questions/17063000/ng-model-for-input-type-file-with-directive-demo). It binds a [FileList](https://developer.mozilla.org/en-US/docs/Web/API/FileList) object to the `ng-model` directive. `FileList` is an array-like object not a `File` object. – georgeawg Jan 03 '20 at 07:51
  • 1
    Does this answer your question? [AngularJS Unable to display upload file logs](https://stackoverflow.com/questions/59489233/angularjs-unable-to-display-upload-file-logs) – Ashish Bakwad Jan 03 '20 at 07:53
  • @georgeawg can you explain further, like what else do I need to do? –  Jan 03 '20 at 07:56
  • @AshishBakwad no, the controller codes are the same –  Jan 03 '20 at 07:56

0 Answers0