Skip to content

Commit a4e889b

Browse files
committed
Validating Image Size Before Uploading
1 parent d6d7a1e commit a4e889b

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

website/user_profile/templates/create.html

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
</div>
3333
<hr>
3434
<div>
35-
{% render_field form.image class="form-control mt-4" style="background:white;" placeholder="Select An Image To Upload" %}
35+
{% render_field form.image class="form-control mt-4" style="background:white;" placeholder="Select An Image To Upload" onchange="ValidateSize(this)" type="file"%}
3636
</div>
3737
<hr>
3838
</table>
@@ -49,4 +49,18 @@
4949
</div>
5050

5151
</body>
52+
53+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
54+
<script>
55+
function ValidateSize(file) {
56+
var FileSize = file.files[0].size / 1024 / 1024; // in MB
57+
if (FileSize > 2) {
58+
alert('File size exceeds 2 MB');
59+
$(file).val('');
60+
} else {
61+
62+
}
63+
}
64+
</script>
65+
5266
{% endblock %}

0 commit comments

Comments
 (0)