Restrict attachments size on single catalog item

MonicaW
Tera Guru

I have a catalog item where we want to restrict the size of all attachments to 12 MB total.  I tried using the attachment variable, but it only allows the selection of ONE attachment.  I want the ability to add multiple attachments using the built-in "add attachments" on the catalog item itself and have it calculate the size of all of them and restrict it to 12 MB.

 

I found the following code that I put in an onSubmit script, but it's not working - perhaps I need to change more elements than the var maxAttachmentSizeMB = 12; line?

>>>>>>>>>>>>>>>>>>>>>>>>>

function onSubmit() {
var attachments = this.angular.element("#sc_cat_item").scope().attachments; // Adjust element ID as needed
var submitForm = false;
var maxAttachmentSizeMB = 12; // Set your desired limit in MB

attachments.forEach(function(attachment){
var sizeBytes = parseInt(attachment.size_bytes);
var sizeMB = sizeBytes / (1024 * 1024); // Convert bytes to MB.
var sizeString = String(attachment.size_bytes); // Get the size as a string to include the units
alert(sizeString);

if(sizeMB > maxAttachmentSizeMB){
alert(attachment.file_name + ' is too large. Maximum allowed size is ' + maxAttachmentSizeMB + ' MB.');
submitForm = false;
}
});
return submitForm;
}

20 REPLIES 20

Thank you, I will try this and let you know.

Also, I assume I would change the following line to 12000 since I want to restrict all file attachments to 12 MB?

 

var maxSizeKB = 100; //KB limit 

@MonicaW 

 

The value 12000 is close, but it's not exactly 12 MB. It's approximately 11.72 MB. If you want to be precise, use 12288.

Hi Rafael:

I set this up using 12288 in the ScriptInclude.  Then in my onSubmit script, I used exactly what you provided.  Were there any changes necessary in the onSubmit script that I should be using?  I'm getting a Error: There is a JavaScript error in your browser console .

Thank you.

Hi @MonicaW 

 

When you submit your catalog and the error msg appears, 

 

Press  F12 and show me a screenshot of console log with the msg error