How can File Upload restrict to ONLY showing a single file type?

The "Allow File Extensions (Gravity Forms AdvacnedFields->“File upload” fieldtype) seems to only check filename extensions on SUBMIT.

Can this be used to filer the upload options displayed the the user?

After all, javascript does allow this:

At the above link, you can change to accept=“.xlsx” and see only folders and .xlsx files, for example, when you click in the try-it area at right.

(I’ve searched the prior conversations and docs every way I can imagine. Sorry if this newbie has missed the obvious. My app is “working” - but just showing the user far too many files to choose from. Even image file extensions seem to result in no change to the file options displayed to the user).

Thanks

To filter the upload options displayed to the user based on file extensions, you can use the “accept” attribute in the file input field, as you mentioned. The “accept” attribute is a standard HTML attribute specifying the types of files the user can select in the file upload dialog box.

For example, to allow only .xlsx files, you can set the accept attribute to “.xlsx” as follows:

<input type="file" name="myfile" accept=".xlsx">

This will filter the displayed options only to show folders and .xlsx files, just like you saw in the try-it area on the Mozilla Developer Network page.

You can add the “accept” attribute to the file upload field in Gravity Forms by adding the following code to your child theme’s functions.php file or a custom plugin:

// Add the "accept" attribute to the file upload field in Gravity Forms
add_filter( 'gform_field_content', 'add_accept_attribute', 10, 5 );
function add_accept_attribute( $content, $field, $value, $lead_id, $form_id ) {
    // Check if the field is a file upload field
    if ( $field->type == 'fileupload' ) {
        // Get the allowed extensions set in the field settings
        $extensions = rgar( $field, 'allowedExtensions' );
        // If there are allowed extensions
        if ( ! empty( $extensions ) ) {
            // Concatenate the extensions with a period and a comma
            $accept = '.' . implode( ',.', $extensions );
            // Replace the existing file input field with one that includes the "accept" attribute
            $content = str_replace( '<input', '<input accept="' . $accept . '"', $content );
        }
    }
    // Return the modified field content
    return $content;
}

This code adds the “accept” attribute to the file upload field based on the allowed extensions set in the field settings.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.