Setting Limitations for Size and Number of Files

ActiveX/Java Uploader allows not only filtering files by type, or limiting images but also limiting size and count of files. It is extremely useful when you:

  • want to allocate limited space on your server for users
  • provide file hosting service, which does not allow uploading large files

Restricting Number of Files for Upload

Note

ActiveX/Java Uploader Express restricts the maximum number of user-selected files to 100. See the Upload Suite Editions topic for details.

To limit the maximum and minimum number of files allowed for the upload in a single upload session, use the Restrictions.MaxFileCountMaxFileCount (ASP.NET)MaxFileCount (PHP)maxFileCount (JavaScript) and Restrictions.MinFileCountMinFileCount (ASP.NET)MinFileCount (PHP)minFileCount (JavaScript) properties respectively. If a user selects more files than specified by the Restrictions.MaxFileCount property, an error message (which can be customized using the Messages.MaxFileCountExceededMaxFileCountExceeded (ASP.NET)MaxFileCountExceeded (PHP)maxFileCountExceeded (JavaScript) property) appears. If a user selects fewer files than the Restrictions.MinFileCount property value, the Upload button is disabled and files cannot be uploaded.

To remove both limitations, set Restrictions.MaxFileCount to 0 and Restrictions.MinFileCount to 1.

This example shows how to limit number of files from 3 to 10:

ASP.NET
<aur:Uploader ID="Uploader1" runat="server">
    <Restrictions  MinFileCount="3" MaxFileCount="10"/>
    <Messages MaxFileCountExceeded="You can add not more than 10 files."/>
</aur:Uploader>
PHP
$uploader = new Uploader("Uploader1");
$uploader->getRestrictions()->setMinFileCount(3);
$uploader->getRestrictions()->setMaxFileCount(10);
$uploader->getMessages()->setMaxFileCountExceeded("You can add not more than 10 files.");
JavaScript
var u = $au.uploader({
    id: 'Uploader1',
    restrictions: {minFileCount: 3, maxFileCount: 10},
    messages: {maxFileCountExceeded: 'You can add not more than 10 files.'}
});

Restricting Upload Size

To set a limit on a total size of files selected for the upload, ActiveX/Java Uploader exposes the Restrictions.MaxTotalFileSizeMaxTotalFileSize (ASP.NET)MaxTotalFileSize (PHP)maxTotalFileSize (JavaScript) property. When the sum of selected file sizes exceeds this value, an error specified with the Messages.MaxTotalFileSizeExceededMaxTotalFileSizeExceeded (ASP.NET)MaxTotalFileSizeExceeded (PHP)maxTotalFileSizeExceeded (JavaScript) property is displayed.

If you do not need to restrict the total file size, set Restrictions.MaxTotalFileSize to 0.

This limitation is applied to sizes of original files only. It does not take into account sizes of files created by ActiveX/Java Uploader from original ones (downsized copies of images, associated icons, compressed ZIP files) because there is no way to know their sizes before upload process starts. Therefore, the amount of the actually uploaded bytes may noticeably differ from this value. So the Restrictions.MaxTotalFileSize property is useful when you upload original files without applying any client-side file transformations. Otherwise, if it is important to limit the POST request size, the only way is to configure maximum acceptable POST request length in the server settings.

This example shows how to limit upload size to 100 Mb:

ASP.NET
<aur:Uploader ID="Uploader1" runat="server">
    <Restrictions MaxTotalFileSize="104857600" />
    <Messages MaxTotalFileSizeExceeded="You are not allowed for uploading more than 100 MB."/>
</aur:Uploader>
PHP
$uploader = new Uploader("Uploader1");
$uploader->getRestrictions()->setMaxTotalFileSize(104857600);
$uploader->getMessages()->setMaxTotalFileSizeExceeded("You are not allowed for uploading more than 100 MB.");
JavaScript
var u = $au.uploader({
    id: 'Uploader1',
    restrictions: {maxTotalFileSize: 104857600},
    messages: {maxTotalFileSizeExceeded: 'You are not allowed for uploading more than 100 MB.'},
});

Restricting Single File Size

Note

ActiveX/Java Uploader Express restricts the maximum size of each user-selected file to 100 Mb. ActiveX/Java Uploader Professional restricts the maximum size of each user-selected file to 2 Gb. See the Upload Suite Editions topic for details.

You can set a limitation not only for total size of the files selected for the upload, but also for a single file size. Both lower and upper limitations can be specified via the Restrictions.MinFileSizeMinFileSize (ASP.NET)MinFileSize (PHP)minFileSize (JavaScript) and Restrictions.MaxFileSizeMaxFileSize (ASP.NET)MaxFileSize (PHP)maxFileSize (JavaScript) properties respectively. Files which are too large or too small cannot be selected, thus, they are invisible by default. Even if a user clicks Total files and tries to add such files to the upload list, ActiveX/Java Uploader displays an error message specified with the Messages.FileSizeTooSmallFileSizeTooSmall (ASP.NET)FileSizeTooSmall (PHP)fileSizeTooSmall (JavaScript) or Messages.MaxFileSizeExceededMaxFileSizeExceeded (ASP.NET)MaxFileSizeExceeded (PHP)maxFileSizeExceeded (JavaScript) property. Additionally, each inappropriate file is equipped with a tooltip containing the same error message text.

Both these properties should be set to 0 when no file size limitations are required.

In the following example files smaller than 200 Kb or larger than 2 Mb are denied:

ASP.NET
<aur:Uploader ID="Uploader1" runat="server">
    <Restrictions MinFileSize="204800" MaxFileSize="2097152" />
    <Messages FileSizeTooSmall="File is too small. You can add files not smaller than 200 KB."
        MaxFileSizeExceeded="File is too large. You can add files not larger than 2 MB."/>
</aur:Uploader>
PHP
$uploader = new Uploader("Uploader1");
$uploader->getRestrictions()->setMinFileSize(204800);
$uploader->getRestrictions()->setMaxFileSize(2097152);
$uploader->getMessages()->setFileSizeTooSmall("File is too small. You can add files not smaller than 200 KB.");
$uploader->getMessages()->setMaxFileSizeExceeded("File is too large. You can add files not larger than 2 MB.");
JavaScript
var u = $au.uploader({
    id: 'Uploader1',
    restrictions: {minFileSize: 204800, maxFileSize: 2097152},
    messages: {fileSizeTooSmall: 'File is too small. You can add files not smaller than 200 KB.',
               maxFileSizeExceeded: 'File is too large. You can add files not larger than 2 MB.'}
});
Important

As limitations specified in ActiveX/Java Uploader are checked only on the client side, you should not interpret them as a reliable protection. A potential malicious user can bypass all these limitations (by emulating ActiveX/Java Uploader or modifying its parameters in a local copy of a page). That is why it is highly recommended to implement server-side verification of uploaded files in addition to ActiveX/Java Uploader file tests.

In other words, all limitation features discussed in this topic should be used solely for user's convenience. They should not be interpreted as a serious protection from malicious users.

See Also

Reference

Manual