On Drupal 7 and 8 when selecting a file with an invalid file extension using the managed file field, javascript's onchange
event callback Drupal.file.validateExtension()
is called. This callback:
- removes error messages that were possible inserted into the DOM earlier,
- detects a wrong file extension,
- inserts a new error message into the DOM,
- sets the field's value to an empty string and
- stops propagating the event.
On Internet Explorer 11 programmatically resetting the value using this.value = '';
fires the onchange
event again. This time:
- the previous error message is being removed (the user will never know what went wrong),
- the field has no value, so no wrong file extension is being detected and
- the event propagating is not being stopped (other
onchange
callbacks like D8'sDrupal.file.triggerUploadButton
will get fired).
This can be tested using http://jsfiddle.net/lmeurs/2rEzF/ or with Drupal using http://simplytest.me/project/drupal: set permissions for anonymous users to create articles so you can add articles from different browsers (ie. using http://browserstack.com) and test with the image field.
Sidenote: On < IE11 it seems not possible to programmatically reset the field's value.
Possible solution
The best fix I can think of is to immediately exit Drupal.file.validateExtension()
when the field has no value. Use something like:
if (!this.value) return;
Comment | File | Size | Author |
---|---|---|---|
#27 | core-js-file-extension-validate-D10-2301527.patch | 580 bytes | ramprassad |
| |||
#25 | reroll_diff_2301527_23-25.txt | 1.17 KB | ankithashetty |
#25 | 2301527-25.patch | 1.21 KB | ankithashetty |
#23 | core-8-8-js-file-extension-validate-2301527.patch | 1.14 KB | andersmosshall |
#22 | core-7-52-js-file-extension-validate-2301527-22.patch | 648 bytes | aaron.ferris |
Comments
Comment #1
MyAudioDNA CreditAttribution: MyAudioDNA commentedComment #2
MyAudioDNA CreditAttribution: MyAudioDNA commentedComment #3
DrColossos CreditAttribution: DrColossos commentedI can confirm this behavior. I don't know why this has occured, but this is a quite new issue with IE for us. We had a working installation from a customer and suddenly encountered this issue. The described problem does indeed occur as described above. The proposed code does work in IE as well as other browsers. This also goes for the latest 7.x release
Comment #4
DrColossos CreditAttribution: DrColossos commentedComment #5
DrColossos CreditAttribution: DrColossos commentedComment #6
ori.volfo CreditAttribution: ori.volfo commentedSo, for now there is no solution that does not involves changing Drupal core files?
Comment #7
nod_Can you post steps to replicate (including if you're using a special format or just the ones by default) and a description of the error (a screenshot possibly?).
Thanks.
Comment #8
nod_Text from issue summary got removed, restored last version.
Comment #9
lmeurs CreditAttribution: lmeurs commented@ori.volfo: Opposed to PHP functions Javascript functions can easily be overwritten. You could load a modified version of the function from your theme (this only effects the front end theme) or a custom module.
But be careful, this might break your upload forms when functionality of the original function changes in the future (though for D7 not very likely).
Comment #10
nod_The issue summary is awesome :)
Got a patch, haven't even run the thing so it really need testing.
Comment #11
DrColossos CreditAttribution: DrColossos commentedIt's quite similar to what the proposed solution is, only a little cleaner (more restrictive?). We have this successfully in production, so I consider this a good fix.
Comment #12
lmeurs CreditAttribution: lmeurs commentedI just tried the D8 patch from #10 using simplytest.me, but this unfortunately does not work well. Without the patch the throbber only appears and disappears. With the patch the warning message also flashes shortly, so we are getting somewhere! :-) Maybe the event should be canceled before exiting the function? (cannot try this at the moment since I do not have access to a D8 dev installation)
In the meanwhile I attached a patch for D7.
Comment #14
lmeurs CreditAttribution: lmeurs commentedAll it needed was an immediate stop of event propagation, patch attached.
BTW: Please do not get distracted by other D7 + D8 bugs with the file field:
Comment #15
DrColossos CreditAttribution: DrColossos commentedThat looks better than the previous patches. I think this sould be merged into dev since it's a usefull fix no matter what.
Comment #17
swetashahi CreditAttribution: swetashahi as a volunteer and at Srijan | A Material+ Company commentedComment #18
swetashahi CreditAttribution: swetashahi as a volunteer and at Srijan | A Material+ Company commentedThis patch cannot be applied. Moving to needs work.
Comment #21
kristofferwiklund CreditAttribution: kristofferwiklund at Websystem commentedI could not reproduce this on 8.3.x or 8.2.x. At least I get an error message saying that the file type is wrong.
But the bug is there for 7.50. As end user I do not get any error message for IE11, (but for Chrome I get one)
Comment #22
aaron.ferris CreditAttribution: aaron.ferris commentedThe attached patch from #12 does indeed resolve this issue in my instance of Drupal 7, but the patch didn't apply cleanly for me in 7.52. Could just be my implementation but i'm attaching a re-roll in case anyone else comes across the same for this version.
Comment #23
andersmosshall CreditAttribution: andersmosshall at Websystem commentedCreated a patch for drupal 8.8.x from #22 patch
Comment #24
artemboikoThanks it works. Tested on Drupal 8.9.13
Comment #25
ankithashettyRerolled the patch in #23 against the 9.3.x version, thanks!
Comment #26
ramprassad CreditAttribution: ramprassad commentedRerolling the patch for Drupal 10.x core
Comment #27
ramprassad CreditAttribution: ramprassad commentedRerolling the patch for Drupal 10.x core