Skip to main content
A newer version of this page is available. .

Allowed File Extensions

  • 4 minutes to read

ASPxUploadControl allows you to specify the extensions of files that users can upload to the server.

Related API

Member Description
UploadControlValidationSettings.AllowedFileExtensions Specifies the allowed file extensions (server side).
ASPxClientUploadControlValidationSettings.allowedFileExtensions Returns the allowed file extensions (client side).
UploadControlValidationSettings.NotAllowedFileExtensionErrorText Specifies the error text indicating that the specified file’s extension is not allowed.
UploadedFile.IsValid Specifies whether the uploaded file passes validation.
ASPxClientUploadControl.ValidationErrorOccurred Enables you to specify whether the selected file or files are valid and display error text.
ASPxUploadControl.FileUploadComplete Occurs when the file has been uploaded.
ASPxClientUploadControl.FileUploadComplete Occurs on the client after the file has been uploaded.
ASPxUploadControl.FilesUploadComplete Occurs after all selected files have been uploaded to the server.
ASPxClientUploadControl.FilesUploadComplete Occurs on the client after all selected files have been uploaded.

The following code snippets illustrate how to define the UploadControlValidationSettings.AllowedFileExtensions property declaratively or programmatically:

  • Declarative technique:

    <dxuc:ASPxUploadControl 
        ...
        <ValidationSettings 
            AllowedFileExtensions=".jpg, .jpeg, .tif, .bmp, .txt, .doc, .docx, .xls, .xlsx, .pdf, .rtf, .xlsx" 
        ...
        </ValidationSettings>
    </dxuc:ASPxUploadControl>
    
  • Programmatic technique:

    ASPxUploadControl1.ValidationSettings.AllowedFileExtensions=".jpg, .jpeg, .tif, .bmp, .txt, .doc, .docx, .xls, .xlsx, .pdf, .rtf, .xlsx";
    

Note

Users can upload any files if the UploadControlValidationSettings.AllowedFileExtensions property is not specified (or not set to an empty string).

If the uploaded file’s extension is not allowed (not listed in the UploadControlValidationSettings.AllowedFileExtensions property), the ASPxUploadControl‘s validation fails and the control displays the UploadControlValidationSettings.NotAllowedFileExtensionErrorText property’s value within an error frame or alert box when the upload control operates in Standard or Advanced upload modes, respectively.

Note

In Standard mode, the upload control validation is performed only on the server side. In Advanced mode, the upload control validates the uploaded file(s) on the client side before the file is uploaded.

ASPxUploadControl-AllowedFileExtensionsError

The FileUploadCompleteEventArgs.IsValid, ASPxUploadControl.IsValid and ASPxClientUploadControlFileUploadCompleteEventArgs.isValid properties are set to false if the ASPxUploadControl‘s validation fails.

You can use the following events to implement custom validation logic:

The FileUploadCompleteEventArgs.IsValid and ASPxClientUploadControlFileUploadCompleteEventArgs.isValid properties specify whether the uploaded file passes the validation criteria. The FileUploadCompleteEventArgs.ErrorText and ASPxClientUploadControlFileUploadCompleteEventArgs.errorText properties allow you to set the custom error text displayed if the upload control’s validation fails.

Note that the UploadControlValidationSettings.AllowedFileExtensions property is used only for validating the uploaded file’s extension. The UploadControlValidationSettings.AllowedFileExtensions property does not allow you to customize the appearance of the File Extension filter in the Open File dialog window. The browser determines and controls the list of file extensions in the File Extension filter.

ASPxUploadControl-FileExtensionFilter

Examples

  1. The following example uses the ASPxClientUploadControl.ValidationErrorOccurred event to show how to customize an error message for files that failed validation.

    <dx:ASPxUploadControl ID="ASPxUploadControl2" runat="server" Width="320"
        NullText="Select multiple files..." UploadMode="Advanced" ShowUploadButton="True">
        <AdvancedModeSettings EnableMultiSelect="True" EnableFileList="True" />
        <ValidationSettings MaxFileSize="4194304" AllowedFileExtensions=".jpg,.jpeg" MaxFileCount="2" />
        <ClientSideEvents ValidationErrorOccurred="onUploadControlValidationErrorOccured" />
    </dx:ASPxUploadControl>
    <br />
    <dx:ASPxPopupControl ID="PopupControl" runat="server" ClientInstanceName="popupControl" Modal="true" HeaderText="Validation error"
        PopupHorizontalAlign="WindowCenter" PopupVerticalAlign="WindowCenter"
        ShowCloseButton="true" CloseAction="CloseButton" ShowFooter="true">
        <FooterTemplate>
            <dx:ASPxButton ID="ASPxButton1" runat="server" Text="OK" AutoPostBack="False"
                Width="90" CssClass="popup-okButton" ClientSideEvents-Click="function(s, e) { popupControl.Hide(); }">
            </dx:ASPxButton>
        </FooterTemplate>
    </dx:ASPxPopupControl>    
    
  2. The following example shows how to restrict the upload of a file without an extension on the client side.

    function OnTextChanged() {
        var filesForUploading = uploadControl.GetSelectedFiles();
        if (filesForUploading) {
            var allFilesContainExtension = true;
            for (var i = 0; i < filesForUploading.length; i++) {
                var currentFileName = filesForUploading[i].name;
                if (currentFileName == currentFileName.split('.').pop()) {
                    allFilesContainExtension = false;
                    break;
                }
            }
            if (!allFilesContainExtension) {
                alert("Only files with extensions can be uploaded");
                uploadControl.ClearText();
            }
        }
    }