Learn MVC Using Angular File Upload

Jun 23, 2017AngularASP.NET MVCC#
Summarize with AI: Google AI Claude ChatGPT Perplexity Grok

AI links open with a title + excerpt (these tools can't fetch the page themselves) — use "Copy full article" to paste the complete text for a fuller summary.

Share:

Introduction

This article demonstrates learning MVC using Angular File Upload and Bootstrap Filestyle 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 a file is added to the queue, it creates a 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.

Below are the steps that allow you to use Angular File Upload in Angular JS and MVC.

  • Create an MVC Project.
  • Configure Angular File Upload and Bootstrap Filestyle.
  • Work in Angular File Upload.

Create an MVC Project

Open Visual Studio 2017. MVC Go to New menu >Click New and Project. Now it will open the New Project Window. MVC You can select ASP.NET Web Application on Framework 4.5. Enter the name of the project in tje “Solution name” text box and then click OK. MVC One more window should appear. Select MVC Template in this pop-up and click OK. Now start cropping your image.

Configure Angular File Upload and Bootstrap Filestyle

Right click on the project and go to the NuGet package manager. Then search ‘angularfileupload’ in the Browse tab and install it. MVC OR you can download the plugins from here:

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

 

     

Link your Angular configurable file here, using whatever name you gave it.

 

 

Angular Module

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

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

Work in Angular Image Upload

Using a Bootstrap Filestyle directive, create a single uploading attribute.

<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
   
 
Another drop zone with its own settings
 
 
   
   
   
 
 
 
 
 
 

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

   

No files in queue. Start adding some..

 
                                       
NameSizeProgressStatusActions
  {{ item.file.name }}   {{ item.file.size/1024/1024|number:2 }} MB  
 
 
 
                           
 
 
 
 
 
 
 

Queue progress:

 
 
 
 
       
 
 
 
 
 

Angular Controller

Initiate the Fileuploader as an object. This object will load the predefined function.

function FileUploadController(FileUploader) {   var uploader = $scope.uploader = new FileUploader({   url: 'server call'   });   uploader.filters.push({   name: 'customFilter',   fn: function (item, options) {   return this.queue.length < 10;   }   });   uploader.onWhenAddingFileFailed = function (item, filter, options) {   console.info('onWhenAddingFileFailed', item, filter, options);   };   uploader.onAfterAddingFile = function (fileItem) {   console.info('onAfterAddingFile', fileItem);   };   uploader.onAfterAddingAll = function (addedFileItems) {   console.info('onAfterAddingAll', addedFileItems);   };   uploader.onBeforeUploadItem = function (item) {   console.info('onBeforeUploadItem', item);   };   uploader.onProgressItem = function (fileItem, progress) {   console.info('onProgressItem', fileItem, progress);   };   uploader.onProgressAll = function (progress) {   console.info('onProgressAll', progress);   };   uploader.onSuccessItem = function (fileItem, response, status, headers) {   console.info('onSuccessItem', fileItem, response, status, headers);   };   uploader.onErrorItem = function (fileItem, response, status, headers) {   console.info('onErrorItem', fileItem, response, status, headers);   };   uploader.onCancelItem = function (fileItem, response, status, headers) {   console.info('onCancelItem', fileItem, response, status, headers);   };   uploader.onCompleteItem = function (fileItem, response, status, headers) {   console.info('onCompleteItem', fileItem, response, status, headers);   };   uploader.onCompleteAll = function () {   console.info('onCompleteAll');   };   console.info('uploader', uploader);

Angular Directive

You can use Bootstrap Filestyle as an Angular directive. When you select the image using Filestyle, set the element using the directive.

directive('filestyle', filestyle);   functionfilestyle () {   controller.$inject = ['$scope', '$element'];   return {   restrict: 'A',   controller: controller   };   function controller($scope, $element) {   var options = $element.data();   options.classInput = $element.data('classinput') || options.classInput;   $element.filestyle(options);   }   }

Click the 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 one or more filters into the component, and these filters are then 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 and Bootstrap File Style. If you have any queries, please tell me through the comments section, because your comments are very valuable. Happy Coding!

#AngularJS#ASP.NET MVC#Visual Studio

Comments

Be the first to comment.

Leave a comment

Never shown publicly.

Comments are reviewed before appearing publicly.