mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 03:55:55 +01:00
Check file size locally.
This commit is contained in:
@@ -16,9 +16,10 @@ define('uploader', ['csrf', 'translator'], function(csrf, translator) {
|
||||
};
|
||||
|
||||
module.show = function(data, callback) {
|
||||
var fileSize = data.hasOwnProperty('fileSize') && data.fileSize !== undefined ? parseInt(data.fileSize, 10) : false;
|
||||
parseModal({
|
||||
showHelp: data.hasOwnProperty('showHelp') && data.showHelp !== undefined ? data.showHelp : true,
|
||||
fileSize: data.hasOwnProperty('fileSize') && data.fileSize !== undefined ? parseInt(data.fileSize, 10) : false,
|
||||
fileSize: fileSize,
|
||||
title: data.title || '[[global:upload_file]]',
|
||||
description: data.description || '',
|
||||
button: data.button || '[[global:upload]]',
|
||||
@@ -40,13 +41,17 @@ define('uploader', ['csrf', 'translator'], function(csrf, translator) {
|
||||
});
|
||||
|
||||
uploadForm.submit(function() {
|
||||
onSubmit(uploadModal, callback);
|
||||
onSubmit(uploadModal, fileSize, callback);
|
||||
return false;
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
function onSubmit(uploadModal, callback) {
|
||||
module.hideAlerts = function(modal) {
|
||||
$(modal).find('#alert-status, #alert-success, #alert-error, #upload-progress-box').addClass('hide');
|
||||
};
|
||||
|
||||
function onSubmit(uploadModal, fileSize, callback) {
|
||||
function showAlert(type, message) {
|
||||
module.hideAlerts(uploadModal);
|
||||
uploadModal.find('#alert-' + type).translateText(message).removeClass('hide');
|
||||
@@ -57,9 +62,13 @@ define('uploader', ['csrf', 'translator'], function(csrf, translator) {
|
||||
uploadModal.find('#upload-progress-bar').css('width', '0%');
|
||||
uploadModal.find('#upload-progress-box').show().removeClass('hide');
|
||||
|
||||
if (!uploadModal.find('#fileInput').val()) {
|
||||
var fileInput = uploadModal.find('#fileInput');
|
||||
if (!fileInput.val()) {
|
||||
return showAlert('error', '[[uploads:select-file-to-upload]]');
|
||||
}
|
||||
if (hasValidFileSize(fileInput[0], fileSize) === false) {
|
||||
return showAlert('error', '[[error:file-too-big, ' + fileSize + ']]');
|
||||
}
|
||||
|
||||
uploadModal.find('#uploadForm').ajaxSubmit({
|
||||
headers: {
|
||||
@@ -107,9 +116,11 @@ define('uploader', ['csrf', 'translator'], function(csrf, translator) {
|
||||
return response;
|
||||
}
|
||||
|
||||
module.hideAlerts = function(modal) {
|
||||
$(modal).find('#alert-status, #alert-success, #alert-error, #upload-progress-box').addClass('hide');
|
||||
};
|
||||
function hasValidFileSize(fileElement, maxSize) {
|
||||
if (window.FileReader && maxSize) {
|
||||
return fileElement.files[0].size <= maxSize * 1000;
|
||||
}
|
||||
}
|
||||
|
||||
return module;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user