Annotation of loncom/javascriptlib/file_upload.js, revision 1.2
1.1 musolffc 1: /*
2: The LearningOnline Network with CAPA
3: JavaScript functions handling file uploading
4:
1.2 ! raeburn 5: $Id: file_upload.js,v 1.1 2015/06/18 20:19:18 musolffc Exp $
1.1 musolffc 6:
7: Copyright Michigan State University Board of Trustees
8:
9: This file is part of the LearningOnline Network with CAPA (LON-CAPA).
10:
11: LON-CAPA is free software; you can redistribute it and/or modify
12: it under the terms of the GNU General Public License as published by
13: the Free Software Foundation; either version 2 of the License, or
14: (at your option) any later version.
15:
16: LON-CAPA is distributed in the hope that it will be useful,
17: but WITHOUT ANY WARRANTY; without even the implied warranty of
18: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19: GNU General Public License for more details.
20:
21: You should have received a copy of the GNU General Public License
22: along with LON-CAPA; if not, write to the Free Software
23: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24:
25: /home/httpd/html/adm/gpl.txt
26:
27: http://www.lon-capa.org/
28: */
29:
30:
31:
32: /*
33: This function accepts a file input element and a maximum upload size. If the
34: file(s) is too large, an alert is shown and the input is cleared. It is better
35: to do this check on the client before uploading.
36:
37: INPUT:
38: fileInput -
1.2 ! raeburn 39: <input type="file" class="LC_flUpload" />
! 40: Using the class "LC_flUpload" is needed to use the event handlers below.
1.1 musolffc 41: maxSize -
42: Maximum upload size in bytes. It is usually calculated from quota and
43: disk usage.
44: */
45: function checkUploadSize (fileInput, maxSize) {
46: try {
47: var fileSize = 0;
48: if ('files' in fileInput) {
49: if (fileInput.files.length > 0) {
50: for (var i = 0; i < fileInput.files.length; i++) {
51: fileSize += fileInput.files[i].size;
52: }
53: if (fileSize > maxSize) {
54: alert("File(s) too large to be attached");
55: clearFileInput(fileInput);
56: }
57: }
58: } else { alert("no files in upFiles");}
59: } catch (e) { alert("Error is: " + e); }
60: }
61:
62: /*
63: This function clears the contents of a file input element.
64:
65: INPUT:
66: ctrl -
67: <input type="file" />
68: */
69: function clearFileInput(ctrl) {
70: try {
71: ctrl.value = null;
72: } catch(ex) { }
73: if (ctrl.value) {
74: ctrl.parentNode.replaceChild(ctrl.cloneNode(true), ctrl);
75: }
76: }
77:
78: /*
79: This block adds event listeners to file upload elements. It looks for input
1.2 ! raeburn 80: elements with class="LC_flUpload".
1.1 musolffc 81:
1.2 ! raeburn 82: <input type="file" class="LC_flUpload" />
1.1 musolffc 83:
84: It also looks for a hidden element with id="free_space" that contains the maximum
85: upload size.
86:
87: <input type="hidden" id="free_space" value="$free_space" />
88:
89: When the contents of the input element change, the function checkUploadSize()
90: checks if it is allowed based on size.
91: */
92: $( document ).ready(function() {
93: var maxSize = $( "#free_space" ).val();
1.2 ! raeburn 94: var upload_elements = $( ".LC_flUpload" );
1.1 musolffc 95: for (var i=0; i<upload_elements.length; i++) {
96: upload_elements[i].addEventListener( "change", function(){
97: checkUploadSize(this, maxSize);
98: });
99: }
100: });
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>