Learn MVC Using Angular File Upload

Introduction

This article demonstrates MVC using angular file upload using bootstrap file style in Visual Studio 2017.

Angular File Upload

Angular file upload supports native Html5 uploads. It works with any server side platform which supports standard html form uploads. When file is added to the queue, it creates file item and uploader options are into this object. After, the items in the queue are ready for uploading.

Features

  • Drag-n-drop upload
  • Upload progress
  • Validation file filter
  • File upload queue

Follow the steps you can use angular file upload in angular JS in MVC

  • Create MVC Project
  • Configure Angular file upload & Bootstrap filestyle
  • Work in Angular file upload

Create MVC Project

Open the visual studio 2017

MVC

Go to New menu >Click New & project. Now it will open New Project Window

MVC

You can select ASP.NET Web Application on Framework 4.5. Enter the name of project in “Solution name” textbox then click ok button.

MVC

One more window should be appearing. Select MVC Template in this popup & Click ok button. Now start cropping image.

Configure Angular File Upload & Bootstrap Filestyle

Right click above the project and go to the NuGet package manager then search ‘angularfileupload’ in browse tab & install it.

MVC

OR You can download the plug in from

Open the _Layout.cshtml and must refer the .js file from downloaded folder to this view page

  1. <script src="~/Plugin/angular/angular.min.js"></script>  
  2.     <script src="~/Plugin/angular-ui-router/release/angular-ui-router.min.js"></script>  
  3.     <script src="~/Plugin/angular-file-upload/dist/angular-file-upload.min.js"></script>  
  4.     <script src="~/Plugin/bootstrap-filestyle/src/bootstrap-filestyle.js"></script>  

Link your angular configurable file, whatever you give as the name

  1. <script src="~/App/App.module.js"></script>  
  2. <script src="~/App/App.config.js"></script>  
  3. <script src="~/App/IUController.js"></script>   

Angular Module

You will need to include the module as a dependency on your application.

  1. var uiroute = angular  
  2.     .module('uiroute', ['ui.router','angularFileUpload']);   

If you have any doubts in configuration, visit my articles,

Work in Angular image Upload

Using bootstrap file style directive make it as a single uploading attribute. Based on attribute you can set

  1. <input filestyle="" type="file" data-icon-name="icon-upload" data-button-text="Single" data-class-button="btn btn-default" data-classinput="form-control inline" nv-file-select="" uploader="uploader" class="form-control" />  

Html Code

  1. <div ng-controller="FileUploadController" nv-file-drop="" uploader="uploader" filters="queueLimit, customFilter">  
  2.     <div class="container-fluid">  
  3.         <div class="row">  
  4.             <div class="col-md-3">  
  5.                 <div class="panel">  
  6.                     <div class="panel-body">  
  7.                         <h4 class="page-header mt0">Select files</h4>  
  8.                         <div ng-show="uploader.isHTML5">  
  9.                             <!-- 3. nv-file-over uploader="link" over-class="className"-->  
  10.                             <div nv-file-over="" uploader="uploader" over-class="bg-info" class="box-placeholder my-drop-zone">Base drop zone</div>  
  11.                             <!-- Example: nv-file-drop="" uploader="{Object}" options="{Object}" filters="{String}"-->  
  12.                             <div nv-file-drop="" uploader="uploader" options="{ url: '/foo' }">  
  13.                                 <div nv-file-over="" uploader="uploader" over-class="bg-purple" class="box-placeholder my-drop-zone">Another drop zone with its own settings</div>  
  14.                             </div>  
  15.                         </div>  
  16.                         <!-- Example: nv-file-select="" uploader="{Object}" options="{Object}" filters="{String}"-->  
  17.                         <hr />  
  18.                         <input filestyle="" type="file" data-icon-name="icon-upload" data-button-text="Multiple" data-class-button="btn btn-default" data-class-input="form-control inline" nv-file-select="" uploader="uploader" multiple="" class="form-control" />  
  19.                         <br />  
  20.                         <input filestyle="" type="file" data-icon-name="icon-upload" data-button-text="Single" data-class-button="btn btn-default" data-classinput="form-control inline" nv-file-select="" uploader="uploader" class="form-control" />  
  21.                     </div>  
  22.                 </div>  
  23.             </div>  
  24.             <div style="margin-bottom: 40px" class="col-md-9">  
  25.                 <div class="panel">  
  26.                     <div class="panel-body">  
  27.                         <p ng-if="uploader.queue.length" class="pull-right label label-info">Queue length: {{ uploader.queue.length }}</p>  
  28.                         <h4 class="page-header mt0">Upload queue</h4>  
  29.                         <p ng-if="!uploader.queue.length" class="lead text-center">No files in queue. Start adding some..</p>  
  30.                         <div ng-if="uploader.queue.length">  
  31.                             <table ng-if="uploader.queue.length" class="table">  
  32.                                 <thead>  
  33.                                     <tr>  
  34.                                         <th width="50%">Name</th>  
  35.                                         <th ng-show="uploader.isHTML5">Size</th>  
  36.                                         <th ng-show="uploader.isHTML5">Progress</th>  
  37.                                         <th>Status</th>  
  38.                                         <th>Actions</th>  
  39.                                     </tr>  
  40.                                 </thead>  
  41.                                 <tbody>  
  42.                                     <tr ng-repeat="item in uploader.queue">  
  43.                                         <td>  
  44.                                             <strong>{{ item.file.name }}</strong>  
  45.                                         </td>  
  46.                                         <td ng-show="uploader.isHTML5" nowrap="">{{ item.file.size/1024/1024|number:2 }} MB</td>  
  47.                                         <td ng-show="uploader.isHTML5">  
  48.                                             <div style="margin-bottom: 0;" class="progress progress-xs">  
  49.                                                 <div role="progressbar" ng-style="{ 'width': item.progress + '%' }" class="progress-bar"></div>  
  50.                                             </div>  
  51.                                         </td>  
  52.                                         <td class="text-center">  
  53.                                             <span ng-show="item.isSuccess">  
  54.                                                 <em class="fa fa-check fa-fw"></em>  
  55.                                             </span>  
  56.                                             <span ng-show="item.isCancel">  
  57.                                                 <em class="fa fa-ban-circle fa-fw"></em>  
  58.                                             </span>  
  59.                                             <span ng-show="item.isError">  
  60.                                                 <em class="fa fa-times fa-fw"></em>  
  61.                                             </span>  
  62.                                         </td>  
  63.                                         <td nowrap="">  
  64.                                             <button type="button" ng-click="item.upload()" ng-disabled="item.isReady || item.isUploading || item.isSuccess" class="btn btn-info btn-xs">  
  65.                                                 <span class="icon-cloud-upload mr"></span>Upload  
  66.                                             </button>  
  67.                                             <button type="button" ng-click="item.cancel()" ng-disabled="!item.isUploading" class="btn btn-warning btn-xs">  
  68.                                                 <span class="icon-cross mr"></span>Cancel  
  69.                                             </button>  
  70.                                             <button type="button" ng-click="item.remove()" class="btn btn-danger btn-xs">  
  71.                                                 <span class="icon-trash mr"></span>Remove  
  72.                                             </button>  
  73.                                         </td>  
  74.                                     </tr>  
  75.                                 </tbody>  
  76.                             </table>  
  77.                         </div>  
  78.                     </div>  
  79.                 </div>  
  80.                 <div class="panel">  
  81.                     <div class="panel-body">  
  82.                         <div>  
  83.                             <p>Queue progress:</p>  
  84.                             <div style="" class="progress progress-xs">  
  85.                                 <div role="progressbar" ng-style="{ 'width': uploader.progress + '%' }" class="progress-bar"></div>  
  86.                             </div>  
  87.                         </div>  
  88.                         <button type="button" ng-click="uploader.uploadAll()" ng-disabled="!uploader.getNotUploadedItems().length" class="btn btn-info btn-s">  
  89.                             <span class="icon-cloud-upload mr"></span>Upload all  
  90.                         </button>  
  91.                         <button type="button" ng-click="uploader.cancelAll()" ng-disabled="!uploader.isUploading" class="btn btn-warning btn-s">  
  92.                             <span class="icon-cross mr"></span>Cancel all  
  93.                         </button>  
  94.                         <button type="button" ng-click="uploader.clearQueue()" ng-disabled="!uploader.queue.length" class="btn btn-danger btn-s">  
  95.                             <span class="icon-trash mr"></span>Remove all  
  96.                         </button>  
  97.                     </div>  
  98.                 </div>  
  99.             </div>  
  100.         </div>  
  101.     </div>  
  102. </div>   

Angular Controller

Initiate the Fileuploader to object. This object will load the predefined function. 

  1. function FileUploadController(FileUploader) {  
  2.         var uploader = $scope.uploader = new FileUploader({  
  3.             url: 'server call'  
  4.         });  
  5.         uploader.filters.push({  
  6.             name: 'customFilter',  
  7.             fn: function (item, options) {  
  8.                 return this.queue.length < 10;  
  9.             }  
  10.         });  
  11.         uploader.onWhenAddingFileFailed = function (item, filter, options) {  
  12.             console.info('onWhenAddingFileFailed', item, filter, options);  
  13.         };  
  14.         uploader.onAfterAddingFile = function (fileItem) {  
  15.             console.info('onAfterAddingFile', fileItem);  
  16.         };  
  17.         uploader.onAfterAddingAll = function (addedFileItems) {  
  18.             console.info('onAfterAddingAll', addedFileItems);  
  19.         };  
  20.         uploader.onBeforeUploadItem = function (item) {  
  21.             console.info('onBeforeUploadItem', item);  
  22.         };  
  23.         uploader.onProgressItem = function (fileItem, progress) {  
  24.             console.info('onProgressItem', fileItem, progress);  
  25.         };  
  26.         uploader.onProgressAll = function (progress) {  
  27.             console.info('onProgressAll', progress);  
  28.         };  
  29.         uploader.onSuccessItem = function (fileItem, response, status, headers) {  
  30.             console.info('onSuccessItem', fileItem, response, status, headers);  
  31.         };  
  32.         uploader.onErrorItem = function (fileItem, response, status, headers) {  
  33.             console.info('onErrorItem', fileItem, response, status, headers);  
  34.         };  
  35.         uploader.onCancelItem = function (fileItem, response, status, headers) {  
  36.             console.info('onCancelItem', fileItem, response, status, headers);  
  37.         };  
  38.         uploader.onCompleteItem = function (fileItem, response, status, headers) {  
  39.             console.info('onCompleteItem', fileItem, response, status, headers);  
  40.         };  
  41.         uploader.onCompleteAll = function () {  
  42.             console.info('onCompleteAll');  
  43.         };  
  44.   
  45.         console.info('uploader', uploader);   

Angular Directive

You can use bootstrap filestyle as angular directive. When you select the image using filestyle, set the element to the <img-crop> directive 

  1. .directive('filestyle', filestyle);  
  2.   
  3.     function filestyle() {  
  4.   
  5.         controller.$inject = ['$scope''$element'];  
  6.         return {  
  7.             restrict: 'A',  
  8.             controller: controller  
  9.         };  
  10.   
  11.         function controller($scope, $element) {  
  12.             var options = $element.data();  
  13.             options.classInput = $element.data('classinput') || options.classInput;  
  14.   
  15.             $element.filestyle(options);  
  16.         }  
  17.     }   

Click F5 button and Run the Application. Now it will appear in the browser and you can see the result.

Output 1

MVC

When you selected or dropped into the component, one or more filters are applied. Files which pass all filters are added to the queue.

Output 2

MVC

This creates instance uploaders which are copied into the object.

Output 3

MVC

Image queue is ready to upload the selected image.

Conclusion

In this article, we have learned about mvc using angular file upload using bootstrap file style. If you have any queries, please tell me through the comments section, because your comments are very valuable.

Happy Coding!...


Similar Articles