Allowed File Extensions

  • 4 minutes to read

The ASPxUploadControl allows specifying the extensions of files end-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(s) are valid and provide an 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 the selected files have been uploaded to the server.
ASPxClientUploadControl.FilesUploadComplete Occurs on the client after all selected files has been uploaded.

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

  • Declarative approach.

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

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

End-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 implement custom validation logic using the following events:

The FileUploadCompleteEventArgs.IsValid and ASPxClientUploadControlFileUploadCompleteEventArgs.isValid properties specify whether the uploaded file passes the validation criteria. The FileUploadCompleteEventArgs.ErrorText and ASPxClientUploadControlFileUploadCompleteEventArgs.errorText allow you to set the custom error text 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 illustrates how to customize an error message for files that failed the validation using the ASPxClientUploadControl.ValidationErrorOccurred event.

    <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 uploading files without 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 extentions can be uploaded");
                uploadControl.ClearText();
            }
        }
    }