Problem/Motivation
Drupal's managed_file form #type allows files of any size to be sent to the server. A server-size limit exists, but the user is not informed of the issue until waiting for the possibly large file to be uploaded. Testing a file's size on the client prior to upload is totally doable in the majority of browsers currently in use, and it is possible to gracefully skip this check, falling back to the current behavior, on IE 8 and 9.
Proposed resolution
Make the following changes:
- Determine the maximum allowed size for a given managed_file form element, including as configured on file field widgets, and include this amount as a data- attribute of the file input.
- Add javascript to compare the size of files to this attribute, and prevent form submission if appropriate.
User interface changes
This implies a new validation error, originating in JavaScript, may be presented to the user. Its details would need to be worked out.
API changes
None anticipated
Data model changes
None anticipated
Beta phase evaluation
| Issue category | Feature request because it is new functionality |
|---|---|
| Issue priority | Normal because it only enhances efficiency and UX |
| Unfrozen changes | Frozen because it is a feature and would require additional JavaScript and php logic |
| Comment | File | Size | Author |
|---|
Issue fork drupal-2533896
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #1
mbayntonComment #2
mbayntonComment #3
mbayntonhttp://jsfiddle.net/3jeq6nLL/2/ illustrates a more flexible way to respond when it is determined a file is too large. For example, using this method, it would be possible for a script capable of generating a rescaled version of an image to do that instead of stopping the submission dead.
Comment #4
mbayntonAll js file validation is currently broken by 2235977: the selectors in settings.file.elements match 0 DOM elements, so event listeners are not added to the inputs, It will not be possible to implement this using the same patterns as the file extension validation until this one is resolved.
Comment #5
dave reidComment #7
kevin.dutra commentedUpdating status, since this can't be done until #2235977: JS Client-side file validation is broken (because ajaxPageState is broken?) is resolved.
Comment #13
anas_maw commentedHere is a patch to start from, still needs many improvements but it's working fine on 8.6.x.
To get this patch works, first you should apply this patch #2235977-128: JS Client-side file validation is broken (because ajaxPageState is broken?)
Comment #14
anas_maw commentedUpdated patch
Comment #15
anas_maw commentedUpdated patch
Comment #18
douggreen commentedRerolled, this applies to 8.7.x after https://www.drupal.org/project/drupal/issues/2235977#comment-13305251.
Comment #19
tim bozeman commentedI like it!
I think some of these
testFilevariables need to plural so that listing multiple files works. Also, can we conditionally show each message please?Comment #20
douggreen commentednit:
@returnsshould be@returnComment #23
acbramley commentedHere's a reroll of #19 against Patch 139 on #2235977: JS Client-side file validation is broken (because ajaxPageState is broken?)
Comment #29
diaodiallo commentedHi all,
I am having this same issue with Drupal 10.2.0, when I upload a file which too large (150mb) than the maximum (10mb) nothing happen but was supposed to have error message that the file is larger than allowed. However a size like 15mb trigger the error message.
Thanks for any helping idea.
Comment #30
oleksandr.s commentedHi, here is patch for Drupal version 10.3.1. Should work with 10x versions.
Probably also works with 11x versions.
Patch provides client side validation to check file size.
Comment #31
smustgrave commentedFixes should be in MRs against 11.x
Thanks
Did not review.
Comment #33
oleksandr.s commentedThank you for advice, I've created issue fork and created MR.
Also, updated a patch for version 10.3.1
Comment #34
oleksandr.s commentedComment #35
smustgrave commentedLeft comments on MR but tagging for test coverage
Comment #38
shriaasI am facing this issue with my webfrom, the patch worked for me with a minor change.
My version of webfrom is use the old syntax:
$element['#upload_validators']['file_validate_size']instead of$element['#upload_validators']['FileSizeLimit']['fileLimit'].I am not sure if it would be a good idea to add this as a check in the MR, since this is deprecated and will be removed in 11.0.0 (https://www.drupal.org/node/3363700), but in case someone wants to fix this for their Drupal 10 or older site, replace these lines in /core/modules/file/src/Element/ManagedFile.php:
With these:
And the rest of the patch should work
Comment #39
neptune-dc commentedI will look into building some tests.
Comment #40
neptune-dc commentedI added some tests and fixed the broken test!
Comment #41
smustgrave commentedSo as a feature request think it's meant to be additive. If it broke existing tests then this could break contrib/custom work I'd imagine so we may need to do some work around BC.
Tagging for sub-maintainer to make sure they weigh in too