version 1.1, 2015/06/18 20:19:18
|
version 1.3, 2019/08/11 14:16:55
|
Line 30 http://www.lon-capa.org/
|
Line 30 http://www.lon-capa.org/
|
|
|
|
|
/* |
/* |
This function accepts a file input element and a maximum upload size. If the |
This function accepts a file input element and the universal part of the id |
file(s) is too large, an alert is shown and the input is cleared. It is better |
used for the hidden input element containing maximum upload size permitted. |
to do this check on the client before uploading. |
If the file(s) is too large, an alert is shown and the input is cleared. |
|
|
INPUT: |
INPUT: |
fileInput - |
fileInput - |
<input type="file" class="flUpload" /> |
<input type="file" class="LC_flUpload" /> |
Using the class "flUpload" is needed to use the event handlers below. |
Using the class "LC_flUpload" is needed to use the event handlers below. |
maxSize - |
sizeItem - |
Maximum upload size in bytes. It is usually calculated from quota and |
<input type="hidden" id="PREFIXsizeItemSUFFIX" value="$maxsize" /> |
disk usage. |
|
|
The PREFIX is empty unless the resource is within a composite page. |
|
|
|
The SUFFIX is empty in cases where there will only ever be one file upload |
|
input element in a web page. Otherwise it will contain a unique identifier, |
|
so different maximum sizes can exist for each upload element. |
|
|
|
The value assigned to the hidden element is the maximum upload size in bytes. |
|
|
|
It is either calculated from quota and disk usage (e.g., upload to a course, |
|
authoring space or portfolio space), or is set by a parameter (e.g., upload |
|
to a dropbox, essayresponse or externalresponse item), or is hard-coded |
|
(e.g., upload to the help request form, or an attachment to a discussion post |
|
or feedback message). |
|
|
*/ |
*/ |
function checkUploadSize (fileInput, maxSize) { |
|
|
function checkUploadSize (fileInput,sizeItem) { |
try { |
try { |
|
var maxSize = getMaxSize(fileInput,sizeItem); |
var fileSize = 0; |
var fileSize = 0; |
if ('files' in fileInput) { |
if ('files' in fileInput) { |
if (fileInput.files.length > 0) { |
if (fileInput.files.length > 0) { |
Line 55 function checkUploadSize (fileInput, max
|
Line 71 function checkUploadSize (fileInput, max
|
clearFileInput(fileInput); |
clearFileInput(fileInput); |
} |
} |
} |
} |
} else { alert("no files in upFiles");} |
} else { alert("no files selected for upload");} |
} catch (e) { alert("Error is: " + e); } |
} catch (e) { alert("Error is: " + e); } |
} |
} |
|
|
Line 69 INPUT:
|
Line 85 INPUT:
|
function clearFileInput(ctrl) { |
function clearFileInput(ctrl) { |
try { |
try { |
ctrl.value = null; |
ctrl.value = null; |
} catch(ex) { } |
} catch(e) { } |
if (ctrl.value) { |
if (ctrl.value) { |
ctrl.parentNode.replaceChild(ctrl.cloneNode(true), ctrl); |
ctrl.parentNode.replaceChild(ctrl.cloneNode(true), ctrl); |
} |
} |
} |
} |
|
|
/* |
/* |
|
This function retrieves the allowed maximum file size for a file input element |
|
INPUT: |
|
fileInput - |
|
<input type="file" /> |
|
sizeItem - |
|
<input type="hidden" id="PREFIXsizeItemSUFFIX" /> |
|
|
|
For upload to a dropbox, essayresponse or externalresponse item, |
|
the PREFIX and SUFFIX are extracted from the id of the file upload |
|
element, by separating the id into parts before and after HWFILE. |
|
|
|
The PREFIX is empty unless the resource is within a composite page. |
|
|
|
*/ |
|
function getMaxSize (fileInput,sizeItem) { |
|
var maxSize = 0; |
|
var sizeId = sizeItem; |
|
var uploadId; |
|
try { |
|
if ($(fileInput).hasClass("LC_hwkfile")) { |
|
uploadId = $(fileInput).attr('id'); |
|
var re = /^(|\w*)HWFILE(.+)$/; |
|
var found = uploadId.match(re); |
|
if (found.length == 3) { |
|
sizeId = found[1]+sizeItem+'_'+found[2]; |
|
} |
|
} |
|
if ( $("#"+sizeId).length) { |
|
if ( $("#"+sizeId).val() > 0) { |
|
maxSize = $("#"+sizeId).val(); |
|
} |
|
} |
|
} |
|
catch(e) { } |
|
return maxSize; |
|
} |
|
|
|
/* |
This block adds event listeners to file upload elements. It looks for input |
This block adds event listeners to file upload elements. It looks for input |
elements with class="flUpload". |
elements with class="LC_flUpload". |
|
|
|
<input type="file" class="LC_flUpload" /> |
|
|
<input type="file" class="flUpload" /> |
It also looks for a hidden element with an id containing: "LC_free_space", |
|
which contains the maximum allowable upload size (bytes). |
|
|
It also looks for a hidden element with id="free_space" that contains the maximum |
<input type="hidden" id="*LC_free_space*" value="$free_space" /> |
upload size. |
|
|
|
<input type="hidden" id="free_space" value="$free_space" /> |
The * before LC_free_space and the * after LC_free_space are PREFIX and SUFFIX. |
|
|
When the contents of the input element change, the function checkUploadSize() |
When the contents of the input element change, the function checkUploadSize() |
checks if it is allowed based on size. |
checks if it is allowed based on size. |
*/ |
*/ |
$( document ).ready(function() { |
$( document ).ready(function() { |
var maxSize = $( "#free_space" ).val(); |
var upload_elements = $( ".LC_flUpload" ); |
var upload_elements = $( ".flUpload" ); |
|
for (var i=0; i<upload_elements.length; i++) { |
for (var i=0; i<upload_elements.length; i++) { |
upload_elements[i].addEventListener( "change", function(){ |
if (getMaxSize(upload_elements[i],'LC_free_space')) { |
checkUploadSize(this, maxSize); |
upload_elements[i].addEventListener( "change", function(){ |
}); |
checkUploadSize(this,'LC_free_space'); |
|
}); |
|
} |
} |
} |
}); |
}); |