How to get uploaded file size in django

Viewed 326

I am uploading a zip file through my form. How I can get the file size i.e in KB / MB so I can display it in my template.

2 Answers

Django has a builti |filesizeformat template filter [Django-doc], you can thus use this with:

{{ file.size|filesizeformat }}

as the documentation says, this will:

Formats the value like a ‘human-readable’ file size (i.e. '13 KB', '4.1 MB', '102 bytes', etc.).

You can solve this by using this custom template tag: https://djangosnippets.org/snippets/1866/

After then use it like this in your template:

{% load sizify  %}

{{ yourFile.size|sizify }}

The file size will then be shown in a human readable format (kb, Mb, Gb)

Related