How to limit the size for file upload


Badge +3

Dear Expert,

 

I want to limit the size for file upload from K2 Smartform.

 

Currently user can upload any size of document for example 15 MB but I want to restrict them to maximum 5 MB.

 

Please help me to implement this....

 

Regards

Shahnawaz Alam


3 replies

Userlevel 1
Badge +8

Hi Shah

 

I don't believe there is any way out of the box to achieve this. The file size property is not currently available for the file attachment control.

 

Have a look at the following forum post for a way to get the file size via JavaScript. I would also suggest logging this as a feature request with K2.

 

http://community.k2.com/t5/K2-blackpearl/How-to-get-file-size-from-an-uploaded-File/m-p/78539#M23077

Userlevel 5
Badge +16

Dear Shah,

 

Please do the following step by step:

 

I've created this view for testing

 

14798i9B7130D1A73DEA0E.png

 

now we need to set some properties for the text box and the button

 

text box control:

 

1- name : txtSize

2- remove watermark

3- width 0%

 

button control:

 

1- name: btnGetSize

2- text: Get Size

3- tooltip: Action Button

 

Add the following code as an expression on the datalable control:

 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("[title='Action Button']").click(function(){



var details = document.getElementsByClassName("file-info file-ellipsis")[0].innerHTML;



var index = details.lastIndexOf(">");
var getsize = details.substring(index+1);



var bytes = getsize.indexOf("bytes");
var KB = getsize.indexOf("KB");
var MB = getsize.indexOf("MB");


if (bytes != -1) {
var b = getsize.replace("bytes", "");
$('input[name="txtSize"]').val((b / 1024) / 1024);
}

if (KB != -1) {
var k = getsize.replace("KB", "");
$('input[name="txtSize"]').val(k / 1024);
}

if (MB != -1) {
var k = getsize.replace("MB", "");
$('input[name="txtSize"]').val(k);
}


$('input[name="txtSize"]').focus();

});
});
</script>

When you click on the Get Size button, it will set the value of the text box.

 

Please Note: the previous code converts the size unit to MB, so in your condition consider the the size will be in MB

 

for example: 

 

I added a file of size 266 bytes, after conversion it will be 0.0002536773681640625 MB

 

If txtSize less than 5

 

Execute Create method

 

ALSO you will have to add a rule to empty the txtSize if the attachment has been deleted

 

When Attachemnt is changed

 

if attachment is empty

 

transfare empty string to txtSize

 

 *****UPDATE*****

 

Add another data lable on the viewform and add an expression on it has the following code to hide the textbox because we can't make it invisible from it's properties, if you make invisible it won't get the size, the following code will makes it's opacity 0 on runtime:

 

<script type="text/javascript">
$(document).ready(function()
{
$('input[name="txtSize"]').css('opacity', '0');
});

</script>

 

 

 

tested on IE 11 and Chrome 42

 

if you face and problem, download and deploy the attached package :)

 

sorry if my code is not organized.

 

wish it helps

 

Userlevel 1
Badge +8

K2 have some enhancement for the attachment control planned for H2 2015 that will include file size limits and file type restrictions.

Reply