Search code examples
angularjsfile-uploadasp.net-core-mvcangularjs-fileupload

Upload File and Data with Angularjs in MVC .net core


<input type="file" ng-model="ch.FileName" id="file" name="myFile" nchange="angular.element(this).scope().saveChapter(this.files)"/>
                

Action Method,Js and other Code


Solution

  • Upload File and Data with Angularjs in MVC .net core

    If you'd like to post binary data with other additional information/data from your angularjs frontend to WebAPI backend, you can try to pass the data through FormData object, like below.

    var formdata = new FormData();
    formdata.append('chapterID', $scope.ch.ChapterID);
    formdata.append('bookID', $scope.ch.BookID);
    formdata.append('chapterName', $scope.ch.FileName);
    //...
    formdata.append('cHFile', $scope.chfile);
    
    $http({
        method: 'POST',
        url: 'your_request_url_here',
        headers: { 'Content-Type': undefined },
        data: formdata
    }).then(function mySuccess(response) {
        //...
    

    In browser developer tool Network tab, can find the data passed as expected

    enter image description here

    Test Result

    enter image description here