Introduction

This article demonstrates learning 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. Plugin/angular/angular.min.js”
  2.  Plugin/angular-ui-router/release/angular-ui-router.min.js
  3.  Plugin/angular-file-upload/dist/angular-file-upload.min.js
  4. Plugin/bootstrap-filestyle/src/bootstrap-filestyle.js

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

  1. App/App.module.js
  2. App/App.config.js
  3. App/IUController.js

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’]);

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

Base drop zone

<!– Example: nv-file-drop=”” uploader=”{Object}” options=”{Object}” filters=”{String}”–>

Another drop zone with its own settings

</div>
</div>
<!– Example: nv-file-select=”” uploader=”{Object}” options=”{Object}” filters=”{String}”–>
<hr />
<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” />
<br />
<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” />
</div>
</div>
</div>

Queue length: {{ uploader.queue.length }}

No files in queue. Start adding some..

Name Size Progress Status Actions
{{ item.file.name }} {{ item.file.size/1024/1024|number:2 }} MB

</div>
</td>
<td class=”text-center”>
<span ng-show=”item.isSuccess”>
<em class=”fa fa-check fa-fw”></em>
</span>
<span ng-show=”item.isCancel”>
<em class=”fa fa-ban-circle fa-fw”></em>
</span>
<span ng-show=”item.isError”>
<em class=”fa fa-times fa-fw”></em>
</span>
</td>
<td nowrap=””>
<button type=”button” ng-click=”item.upload()” ng-disabled=”item.isReady || item.isUploading || item.isSuccess” class=”btn btn-info btn-xs”>
<span class=”icon-cloud-upload mr”></span>Upload
</button>
<button type=”button” ng-click=”item.cancel()” ng-disabled=”!item.isUploading” class=”btn btn-warning btn-xs”>
<span class=”icon-cross mr”></span>Cancel
</button>
<button type=”button” ng-click=”item.remove()” class=”btn btn-danger btn-xs”>
<span class=”icon-trash mr”></span>Remove
</button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>

Queue progress:

</div>
</div>
<button type=”button” ng-click=”uploader.uploadAll()” ng-disabled=”!uploader.getNotUploadedItems().length” class=”btn btn-info btn-s”>
<span class=”icon-cloud-upload mr”></span>Upload all
</button>
<button type=”button” ng-click=”uploader.cancelAll()” ng-disabled=”!uploader.isUploading” class=”btn btn-warning btn-s”>
<span class=”icon-cross mr”></span>Cancel all
</button>
<button type=”button” ng-click=”uploader.clearQueue()” ng-disabled=”!uploader.queue.length” class=”btn btn-danger btn-s”>
<span class=”icon-trash mr”></span>Remove all
</button>
</div>
</div>
</div>
</div>
</div>
</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.         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.     function filestyle() {
  3.         controller.$inject = [‘$scope’‘$element’];
  4.         return {
  5.             restrict: ‘A’,
  6.             controller: controller
  7.         };
  8.         function controller($scope, $element) {
  9.             var options = $element.data();
  10.             options.classInput = $element.data(‘classinput’) || options.classInput;
  11.             $element.filestyle(options);
  12.         }
  13.     }

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!…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s