Action Method,Js a" /> Action Method,Js a" /> Action Method,Js a"/>

Upload File and Data with Angularjs in MVC .net core

768 Views Asked by At
<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

1

There are 1 best solutions below

0
Fei Han On BEST ANSWER

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