web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Pages / Set Maximum Number of ...
Power Pages
Unanswered

Set Maximum Number of File and Size in Attachment on 1 Document

(0) ShareShare
ReportReport
Posted on by 30

Hi All,

 

Is it possible to limit maximum number of files and size in attachment on 1 document? I already try the solution from here, but it only counts the files chosen (not count the current attachment on the document). For example, I restrict the number of files attachment to 3, document A already have 2 attachments, then when I choose file and select 3 additional attachment it should be error because document A already have 2 attachments (currently it not error because it only counts the files chosen (3 files)).

 

Also, for the size limitation in attachment, if I use Maximum File Size (in kilobytes) properties in additional settings of Basic Forms, it will only check 1 chosen file size (not all chosen file and current attachment size on the document). For example, I set maximum file size = 10000, total size of current attachments in document A is 8000, then when I choose file and select 3 file with size 1000, 500, 2000 it should be error because total size of attachment will be 8000 + 1000 + 500 + 2000 = 11500 (currently it not get any error because all 3 files chosen the size is less than 10000, and it not count the existing attachment in document A).

 

Thank you.

Categories:
I have the same question (0)
  • EmadBeshai Profile Picture
    806 Moderator on at

    Hi @devost ,

     

    In addition to what you have found at the mentioned post please apply the below code.

     

    $(document).ready(function() {
    	$('#AttachFile').change(function() {
     var fileList = $('#AttachFile')[0].files;
    		if(fileList.length > 2)
    		{
     $('#AttachFile').val("");
    			 alert("You can not select more than 2 files.");
    		}
     });
    var maxsize=1000; //set your max size
    var totalfilessize=0
    for(var i=0;i<fileList.length;i++){
     var filesize = files[i].size;
    }
    if(totalfilessize >maxsize)
     alert("You exceeded the max size for the selected files.");
    
    
    
    });

     

    Please click Accept as solution if my post helped you solve your issue.

    This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

  • Robby Profile Picture
    30 on at

    Hi @EmadBeshai,

     

    Thank you for additional code you add, I try that but look like it need some minor adjustments, I change the code like below and it worked for checking both file size and number of file when upload attachment. However, it still not counts the existing attachment on my document (for edit record).

    $(document).ready(function() {
    	$('#AttachFile').change(function() {
     var fileList = $('#AttachFile')[0].files;
     var maxSize = 10485760; //set max file size in bytes
     var totalFilesSize = 0;
    
    		if(fileList.length > 5)
    		{
     $('#AttachFile').val("");
    			 alert("You can not select more than 5 files.");
    		}
    
     for(var i = 0; i < fileList.length; i++) {
     var fileSize = fileList[i].size;
     totalFilesSize = totalFilesSize + fileSize;
     }
    
     if (totalFilesSize > maxSize)
     {
     $('#AttachFile').val("");
     alert('You exceeded the 10 MB size for the selected files.')
     }
     });
    });

     

  • EmadBeshai Profile Picture
    806 Moderator on at

    Hi @devost ,

     

    For the files are already uploaded before, you will not be able to get their size directly by using the client side JS, but you will need to send WebApi request to the server retrieve and get the size, I don't recommend that because you will get bad performance and experience, my advise here is to create field and keep it hidden then with every time you will upload file or will remove file recalculate the size based on the number exist at the field and the uploaded files or removed files then place the number of this size into that field, by this way will be able to get the best performance and will have an accurate size for the old uploaded files.

     

    Please let me know if you need more clarification.

     

    If this post helps you with your problem, please mark this answer as Accepted Solution.
    If you like my response, please give it a Thumbs Up.

  • Robby Profile Picture
    30 on at

    Hi @EmadBeshai,

     

    Thanks for idea. I just change the previous JS code, and get the existing file size from 

    data-attachmentfilesize attribute.

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Pages

#1
Jerry-IN Profile Picture

Jerry-IN 71

#2
Fubar Profile Picture

Fubar 62 Super User 2025 Season 2

#3
sannavajjala87 Profile Picture

sannavajjala87 31

Last 30 days Overall leaderboard