We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

limit the number of files upload

Hello, 

I am currently using the Uploader component to upload my files to the server. the problem is that I can not limit the number of files to upload. I would like the user to have a maximum of 3 files to upload and return an error message if he tries to add more than 3.

Thanks in advance and regards,

 

1 Reply

PO Prince Oliver Syncfusion Team March 20, 2019 04:54 PM UTC

Hi Toko, 

Thank you for contacting Syncfusion support. 

We have checked your requirement for adding limit to upload the file into uploader.  We have two solution for this case. 

Solution 1

We have prepared sample based on your requirement, get the selected file length from “selected“ event and check the limit “count“. If the count is higher than the selected file then, call args.cancel and show error message. We have alerted the error message for maximum of 3 files to upload. Please find the sample for your reference. 

onFileSelect(args) { 
    var fileLength = args.filesData.length + this.uploadObj.getFilesData().length; 
    if (fileLength > this.count) { 
        args.cancel = true; 
        document.getElementsByClassName('e-error')[0].innerHTML = 'maximum of ' + this.count + ' files to upload'; 
    } else { 
        document.getElementsByClassName('e-error')[0].innerHTML = ''; 
    } 
} 

  
Solution 2

if you need to exceed the file length and upload only the limited files use the below sample. 

onFileSelect(args) { 
    var fileLength = args.filesData.length + this.uploadObj.getFilesData().length; 
    // if you need upload first 3 files means use this way. 
    if (fileLength > this.count) { 
        args.filesData.splice(this.count, args.filesData.length); 
        alert('maximum of ' + this.count + ' files to upload') 
        document.getElementsByClassName('e-error')[0].innerHTML = 'maximum of ' + this.count + ' files to upload'; 
    } else { 
        document.getElementsByClassName('e-error')[0].innerHTML = ''; 
    } 
} 


Please let us know if you need any further assistance on this. 

Regards, 
Prince 


Loader.
Live Chat Icon For mobile
Up arrow icon