File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -112,5 +112,53 @@ <h1 class="mb-4">User Profile</h1>
112
112
editProfile . style . display = 'block' ;
113
113
}
114
114
}
115
+
116
+ document . getElementById ( 'avatar_file' ) . addEventListener ( 'change' , function ( e ) {
117
+ const file = e . target . files [ 0 ] ;
118
+ if ( ! file ) return ;
119
+
120
+ // Get constraints from your template variables
121
+ const maxSizeMB = "{{ max_file_size_mb }}" ;
122
+ const minDimension = "{{ min_dimension }}" ;
123
+ const maxDimension = "{{ max_dimension }}" ;
124
+ const allowedFormats = "{{ allowed_formats }}" ;
125
+
126
+
127
+ // Check file size
128
+ const fileSizeMB = file . size / ( 1024 * 1024 ) ;
129
+ if ( fileSizeMB > maxSizeMB ) {
130
+ alert ( `File size must be less than ${ maxSizeMB } MB` ) ;
131
+ this . value = '' ;
132
+ return ;
133
+ }
134
+
135
+ // Check file format
136
+ const fileFormat = file . type . split ( '/' ) [ 1 ] ;
137
+ if ( ! allowedFormats . includes ( fileFormat ) ) {
138
+ alert ( `File format must be one of: ${ allowedFormats . join ( ', ' ) } ` ) ;
139
+ this . value = '' ;
140
+ return ;
141
+ }
142
+
143
+ // Check dimensions
144
+ const img = new Image ( ) ;
145
+ img . src = URL . createObjectURL ( file ) ;
146
+
147
+ img . onload = function ( ) {
148
+ URL . revokeObjectURL ( this . src ) ;
149
+
150
+ if ( this . width < minDimension || this . height < minDimension ) {
151
+ alert ( `Image dimensions must be at least ${ minDimension } x${ minDimension } pixels` ) ;
152
+ e . target . value = '' ;
153
+ return ;
154
+ }
155
+
156
+ if ( this . width > maxDimension || this . height > maxDimension ) {
157
+ alert ( `Image dimensions must not exceed ${ maxDimension } x${ maxDimension } pixels` ) ;
158
+ e . target . value = '' ;
159
+ return ;
160
+ }
161
+ } ;
162
+ } ) ;
115
163
</ script >
116
164
{% endblock %}
You can’t perform that action at this time.
0 commit comments