-1

I am trying to check file size upload in jinja2 template in django. I want to check it before it gets uploaded, the code below is a form which will be submitted after clicking the button. It will upload a dataset in browser.

<form method="POST" class="post-form" enctype="multipart/form-data">
   {{ form.as_p }}           
   <button onclick="onSubmitBtn()" type="submit" id="btn-hide" class="btn btn-danger">Upload</button>                
</form>
graj499
  • 87
  • 2
  • 12
  • In the server code check length of the file like this `size = len(request.files["file"])` the size will be in bytes convert that to mb. – Krk Rama Krishna Sep 30 '20 at 08:16

1 Answers1

0

If you mean you want to check on the client-side/browser, before the browser initiates uploading through the network, then you need JavaScript for this. See this question and the accepted answer.

Update (additional info from comments):

It's definitely possible to write some JavaScript that works with the rendered output of {{ form.as_p }}. This MDN doc should help. Also you'll probably need to do a preventDefault() to actually prevent file/form submission when your conditions aren't met.

DJ Ramones
  • 823
  • 1
  • 5
  • 11
  • is it possible to check file size here in {{ form.as_p }} this here in html code – graj499 Sep 30 '20 at 08:30
  • @graj499 it's definitely possible to write some JavaScript that works with the render output of `{{ form.as_p }}`. This [MDN doc](https://developer.mozilla.org/en-US/docs/Web/API/File/Using_files_from_web_applications#Getting_information_about_selected_files) should help. Also you'll probably need to do a [`preventDefault()`](https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault) to actually prevent file/form submission if your conditions aren't met. – DJ Ramones Sep 30 '20 at 08:44