Add And Remove Multiple Images In Angular

Step 1

First, consider the below code for your cshtml page for image uploading.

  1. <div class="col-xs-12" style="border-bottom: 1px solid #dddddd;">  
  2.     <div class="col-xs-4">  
  3.         <div class="col-xs-7"> <label class="file-upload-btn btn btn-primary form-control" id="ImageUpload2">  
  4.   
  5. <input type="file" class="file-upload-input imageUpload" accept="image/*" name="imageFile" id="imggallery" multiple="true" style="width: 95px" onchange="angular.element(this).scope().uploadFiles(this)" id="demo-uploader" ng-model="UserPhoto">  
  6.   
  7. <label for="file-1"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="12" viewBox="0 0 20 17"><path d="M10 0l-5.2 4.9h3.3v5.1h3.8v-5.1h3.3l-5.2-4.9zm9.3 11.5l-3.2-2.1h-2l3.4 2.6h-3.5c-.1 0-.2.1-.2.1l-.8 2.3h-6l-.8-2.2c-.1-.1-.1-.2-.2-.2h-3.6l3.4-2.6h-2l-3.2 2.1c-.4.3-.7 1-.6 1.5l.6 3.1c.1.5.7.9 1.2.9h16.3c.6 0 1.1-.4 1.3-.9l.6-3.1c.1-.5-.2-1.2-.7-1.5z" /></svg> <span>Select File</span></label></label>  
  8.         </div>  
  9.     </div>  
  10.     <div class="col-xs-2" style="width:8.666667%;margin-left:3%;">  
  11.         <div class="form-group"> <button type="submit" ng-click="PostPutCImage()" class="btn btn-primary btn-block">Save</button> </div>  
  12.     </div </div>  

Step 2

Add multiple images in your desgin page and allocate some space for your image list.

  1. $scope.EventImageUpload = function() {  
  2.     $images = $('.imageOutput')  
  3.     $(".imageUpload").change(function(event) {  
  4.         readURL(this);  
  5.     });  
  6.     $scope.listEventimage = [];  
  7.   
  8.     function readURL(input) {  
  9.         //if (input.files.length <=5) {  
  10.         $scope.imagedata = "";  
  11.         if (input.files && input.files[0]) {  
  12.             $.each(input.files, function() {  
  13.                     var reader = new FileReader();  
  14.                     reader.onload = function(e) {  
  15.                         var fileSize = input.files[0].size;  
  16.                         if (fileSize > 2000000) {  
  17.                             $scope.imgupload = "";  
  18.                             $scope.Photo = "";  
  19.                             $('#toast-container04').css({  
  20.                                 "display""block"  
  21.                             });  
  22.                             return false;  
  23.                         }  
  24.                         var canvas = document.createElement("canvas");  
  25.                         var imageElement = document.createElement("img");  
  26.                         imageElement.setAttribute('src', e.target.result);  
  27.                         canvas.width = imageElement.width;  
  28.                         canvas.height = imageElement.height;  
  29.                         var context = canvas.getContext("2d");  
  30.                         context.drawImage(imageElement, 0, 0);  
  31.                         var base64Image = canvas.toDataURL("image/jpeg");  
  32.                         var profile = base64Image.replace(/data:image\/jpeg;base64,/g, '');  
  33.                         var url = input.value;  
  34.                         var ext = url.substring(url.lastIndexOf('.') + 1).toLowerCase();  
  35.                         if (ext == 'jpg') {  
  36.                             ext = "jpeg";  
  37.                         }  
  38.                         var result = e.target.result.replace("data:image/" + ext + ";base64,""");  
  39.                         $scope.imgupload1 = result;  
  40.                         $scope.imagedata = profile;  
  41.                         var $el = $('<div style="float:left;padding-left:20px"><ul class="file-list" ><li class="file template-download fade in" style="width:104%"><img class="file-link" id="Clear" src="' + e.target.result + '" padding=15px width=150px height=150px/><button style="position:relative;width: 39%; margin-bottom: 4%;left: 30%;" class="file-delete-btn delete" id="dltbutton" data-ng-click = ClearImage("' + e.target.result + '") title="Delete">delete</button></li></ul></div>').appendTo('.imageOutput');  
  42.                         $compile($el)($scope);  
  43.                         $scope.listEventimage.push({  
  44.                             Image: $scope.imagedata,  
  45.                             ImageId: 0  
  46.                         });  
  47.                     }  
  48.                 }  
  49.                 $("#eventimage").show(); reader.readAsDataURL(this);  
  50.             });  
  51.     }  
  52. }  

Step 3

Next, add image display list. It is wise if possible to add multiple images at a time.

Step 4

If the user wishes to remove one image, click the "Delete" button, remove data from the database, and then once again load the image data.

After deleting the image, we call this method.

 

  1. $scope.listEventimage = [];  
  2. //$scope.imagedata=imagesvalue from database.  
  3. angular.forEach(imagedata, function(a) {  
  4.     $scope.imagedata = database images.  
  5.     $scope.Imageid = database images id;  
  6.     var $el = $('<div style="float:left;padding-left:20px">' + '<td><img class="file-link" id="Clear" src="' + 'data:image/png;base64, ' + $scope.imagedata + '" padding=15px width=150px height=150px /><button style="position:relative;width: 39%; margin-bottom: 4%;left: 30%;" id="dltbutton" class="file-delete-btn delete" data-ng-click="DeleteCEventImage(' + $scope.Imageid + ')">delete</button></td>' + '</div>').appendTo('.imageOutput');  
  7.     $compile($el)($scope);  
  8.     $scope.listEventimage.push({  
  9.         Image: database images,  
  10.         ImageId: database images id  
  11.     });  
  12. });   

I hope this blog is helpful to you.

Ebook Download
View all
Learn
View all