AngularJS Multiple File Upload example using Web API

AngularJS Multiple File Upload example using Web API

In this Post We Will Explain About is AngularJS Multiple File Upload example using Web API With Example and Demo.Welcome on infinityknow.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to AngularJS Multiple File UploadExample

In this post we will show you Best way to implement AngularJS Upload Multiple Files to ASP.Net Web API, hear for ng-file-upload web apiwith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

I am using simple XMLHttpRequest http protocol methods, in this example simple, to make calls to my web API. Therefore, please simple click the below link just and check the example Web API controller.

READ :  vuejs tab component Dynamic flexible and accessible Vuejs tabs

Include Scripts and Modules

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js"></script>
var myApp = angular.module('liveApp', []);

HTML Markup Lanuage – index.html : Step By Step File Upload Example in AngularJS

<!DOCTYPE html>
<html>
<head>
  <title>Step By Step File Upload Example in AngularJS</title>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js"></script>
</head>

<body ng-app="liveApp">

    <div ng-controller="liveCtrl">
        <input type="file" id="file" name="file" multiple
 onchange="angular.element(this).scope().fileGetDetails(this)" />
        <input type="button" ng-click="uploadFiles()" value="Upload" />
        <p><progress id="myphoto" value="0"></progress></p>
    </div>

</body>
</html

File Upload Example in AngularJS : The Controller and the Scope

<script>
    var liveApp = angular.module('liveApp', []);

    liveApp.controller('liveCtrl', function ($scope) {
        $scope.fileGetDetails = function (e) {

            $scope.files = [];
            $scope.$apply(function () {

               
                for (var i = 0; i < e.files.length; i++) {
                    $scope.files.push(e.files[i])
                }

            });
        };

        // NOW here UPLOAD your THE FILES.
        $scope.uploadFiles = function () {

            //FILL simple FormData WITH all the format FILE DETAILS.
            var data = new FormData();

            for (var i in $scope.files) {
                data.append("uploadedFile", $scope.files[i]);
            }

            // ADD simple LISTENERS.
            var sdataXvhr = new XMLHttpRequest();
            sdataXvhr.addEventListener("progress", updateProgress, false);
            sdataXvhr.addEventListener("load", transferComplete, false);

            // SEND simple FILE DETAILS TO THE API.
            sdataXvhr.open("POST", "/api/fileupload/");
            sdataXvhr.send(data);
        }

        // simple UPDATE loader or PROGRESS BAR.
        function updateProgress(e) {
            if (e.lengthComputable) {
                document.getElementById('myphoto').setAttribute('value', e.loaded);
                document.getElementById('myphoto').setAttribute('max', e.total);
            }
        }

        // message simple CONFIRMATION.
        function transferComplete(e) {
            alert("your simple Files uploaded successfully.");
        }
    });
</script>

angularjs file upload with progress Varible

The second method simple call to uploadFiles() is called when the simple user press the clicks the upload button.To simple upload multipart or multiple the files, first we am using FormData() object to collect the multiple file details from the array $scope.files[]. multipart or multiple Once we have extracted the details, we will create a call to the Web API for upload. To do this we am using http progress XMLHttpRequest. This is one of easy way method to create HTTP progress requests, using both simple API Method Like as a data post to REQUEST GET method and POST method. Here, however we have to create a POST method request, since we am sending jus file or multipart or multiple to the server to upload.

READ :  Vuejs Nested v-for index and Nested v-repeat index Example

$scope.uploadFiles = function ()

Example

I hope you have Got What is angularjs multiple file upload example And how it works.I would Like to have FeadBack From My Blog(infinityknow.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(infinityknow.com) Are Most Always Welcome.

Add a Comment

Your email address will not be published. Required fields are marked *