Skip to content

Commit d760271

Browse files
authored
Show error message if too many files are selected for upload (#2376)
Django's (default) file upload mechanism is limited in the number of files it can handle (independent of limitations on file size.) Consequently, Django enforces a hard limit on the number of files and raises an exception if this is exceeded. (Pull #2036 made this limit configurable.) Note that HTML doesn't allow specifying any limit for multiple-file upload fields. Try to prevent people hitting the limit by accident, by disabling the submit button through JavaScript if too many files are selected. For thoughts about better long-term solutions, see issue #2242.
2 parents 0f93b4a + 6e25831 commit d760271

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

physionet-django/project/templates/project/edit_files_panel.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@
252252
// Check the size of the files to be uploaded and stop the form if the file
253253
// size exceeds the maximum allowed.
254254

255+
let max_files = {{ max_files_per_upload|default:0 }};
256+
255257
// The size is calculated in bytes
256258
files = $('#id_file_field').get(0).files;
257259
let size = 0;
@@ -268,10 +270,19 @@
268270
$('#data_size').text(readableBytes(size))
269271
// Show the message
270272
$('#error_data_size').show();
273+
$('#error_too_many_files').hide();
274+
}
275+
else if (files.length > max_files && max_files > 0) {
276+
// Disable the submit button on the form
277+
$('#upload-files-button-submit').attr("disabled", true);
278+
// Show the message
279+
$('#error_data_size').hide();
280+
$('#error_too_many_files').show();
271281
}
272282
else{
273283
// Hide the message
274284
$('#error_data_size').hide();
285+
$('#error_too_many_files').hide();
275286
// Enable the submit button on the form
276287
$('#upload-files-button-submit').attr("disabled", false);
277288
}

physionet-django/project/templates/project/project_files_form.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ <h5 class="modal-title">Upload Files</h5>
3535
</button>
3636
</div>
3737
<div class="modal-body">
38-
<p>Individual file size limit: {{ individual_size_limit }}.</p>
38+
<p>
39+
{% if storage_type != "GCP" %}
40+
You can select up to {{ max_files_per_upload }} files
41+
to upload at once.
42+
{% endif %}
43+
Each file must be smaller than {{ individual_size_limit }}.
44+
</p>
3945
{% if storage_type == "GCP" %}
4046
<div id="myDropzone"></div>
4147
{% else %}
@@ -48,6 +54,9 @@ <h5 class="modal-title">Upload Files</h5>
4854
<p>The total size of the selected files is <span id="data_size"></span>.<br>
4955
Your remaining storage allowance is {{ storage_info.readable_remaining }}.</p>
5056
</div>
57+
<div id="error_too_many_files" style="display:none" class="alert alert-danger">
58+
Only {{ max_files_per_upload }} files can be uploaded at once.
59+
</div>
5160
</div>
5261
<div class="modal-footer">
5362
{% if storage_type == "GCP" %}

physionet-django/project/views.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,7 @@ def project_files_panel(request, project_slug, **kwargs):
10291029
'is_submitting': is_submitting,
10301030
'is_editor': is_editor,
10311031
'files_editable': files_editable,
1032+
'max_files_per_upload': settings.DATA_UPLOAD_MAX_NUMBER_FILES,
10321033
'individual_size_limit': utility.readable_size(ActiveProject.INDIVIDUAL_FILE_SIZE_LIMIT),
10331034
},
10341035
)
@@ -1149,6 +1150,7 @@ def project_files(request, project_slug, subdir='', **kwargs):
11491150
'project/project_files.html',
11501151
{
11511152
'project': project,
1153+
'max_files_per_upload': settings.DATA_UPLOAD_MAX_NUMBER_FILES,
11521154
'individual_size_limit': utility.readable_size(ActiveProject.INDIVIDUAL_FILE_SIZE_LIMIT),
11531155
'subdir': subdir,
11541156
'parent_dir': parent_dir,

0 commit comments

Comments
 (0)