add browser-side validation of document size (#18599)

This commit is contained in:
Paul Marillonnet 2017-11-16 15:55:16 +01:00
parent 2c2fcfa8a7
commit 0aeef5bc1f
2 changed files with 19 additions and 1 deletions

View File

@ -85,6 +85,7 @@ class Homepage(Documents, SingleTableMixin, CommonUpload):
def get_context_data(self, **kwargs):
ctx = super(Homepage, self).get_context_data(**kwargs)
ctx['include_edit_link'] = settings.INCLUDE_EDIT_LINK
ctx['max_document_size'] = settings.FARGO_MAX_DOCUMENT_SIZE
return ctx
def get_success_url(self):

View File

@ -23,7 +23,16 @@
}
});
$('#send-file input[type=file]').on('change', function() {
$('#send-file input[type=submit]').click();
$('#id_content input[type=file]').change();
});
$('#id_content').on('change', function() {
var fileSize = this.files[0].size;
if (fileSize > {{ max_document_size  }}) {
$('.invalid-file-error').show();
}
else {
$('#send-file input[type=submit]').click();
}
});
$('#send-file button').on('click', function() {
$('#send-file input[type=file]').click();
@ -44,6 +53,14 @@
{{ form.as_p }}
<input type="submit" name="submit" value="Upload">
</div>
<div class="invalid-file-error" style="display: none">
<p>
{% blocktrans with max_size_mb=max_document_size|filesizeformat %}
Error: an uploaded document can't exceed {{ max_size_mb }}.
{% endblocktrans %}
</p>
<p>{% trans "Please try again with a smaller document." %}</p>
</div>
<button>{% trans "Upload a new document" %}</button>
</form>
</div>