PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 31, 2022

[FIXED] How to limit the maximum uploads in Angular

 July 31, 2022     angular, file-upload, javascript     No comments   

Issue

How should i limit the maximum 5 uploads in a single input file in angular

<input type="file" class="form-control" id="inputGroupFile" 
onChange="uploadMultipleFiles($event)" > 

app.component.ts:

uploadMultipleFiles(files) {
  if (files.length > 3) {
   this.alertService.error("length exceeded; files have been truncated");
   let list = new DataTransfer;
   for (let i = 0; i < 3; i++)
    list.items.add(files[i]);
  }
}

Thanks


Solution

Like the below code, you can prevent the limit of file upload.

filesData(files, event): void {
  
    if (event.target.files.length > 5) {
      alert("Only 5 files allow");

      event.preventDefault();
      event.value = ""; // clear the older value 
    } else {
        //Put your Business logic here
    }
  }

Also, pass the $event as an argument to the function on the calling the onChange method.

 <input type="file" multiple #file (change)="filesData($event)" />


Answered By - Meet
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing