jquery.fileupload-validate.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * jQuery File Upload Validation Plugin
  3. * https://github.com/blueimp/jQuery-File-Upload
  4. *
  5. * Copyright 2013, Sebastian Tschan
  6. * https://blueimp.net
  7. *
  8. * Licensed under the MIT license:
  9. * https://opensource.org/licenses/MIT
  10. */
  11. /* global define, require */
  12. (function (factory) {
  13. 'use strict';
  14. if (typeof define === 'function' && define.amd) {
  15. // Register as an anonymous AMD module:
  16. define(['jquery', './jquery.fileupload-process'], factory);
  17. } else if (typeof exports === 'object') {
  18. // Node/CommonJS:
  19. factory(require('jquery'), require('./jquery.fileupload-process'));
  20. } else {
  21. // Browser globals:
  22. factory(window.jQuery);
  23. }
  24. })(function ($) {
  25. 'use strict';
  26. // Append to the default processQueue:
  27. $.blueimp.fileupload.prototype.options.processQueue.push({
  28. action: 'validate',
  29. // Always trigger this action,
  30. // even if the previous action was rejected:
  31. always: true,
  32. // Options taken from the global options map:
  33. acceptFileTypes: '@',
  34. maxFileSize: '@',
  35. minFileSize: '@',
  36. maxNumberOfFiles: '@',
  37. disabled: '@disableValidation'
  38. });
  39. // The File Upload Validation plugin extends the fileupload widget
  40. // with file validation functionality:
  41. $.widget('blueimp.fileupload', $.blueimp.fileupload, {
  42. options: {
  43. /*
  44. // The regular expression for allowed file types, matches
  45. // against either file type or file name:
  46. acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
  47. // The maximum allowed file size in bytes:
  48. maxFileSize: 10000000, // 10 MB
  49. // The minimum allowed file size in bytes:
  50. minFileSize: undefined, // No minimal file size
  51. // The limit of files to be uploaded:
  52. maxNumberOfFiles: 10,
  53. */
  54. // Function returning the current number of files,
  55. // has to be overridden for maxNumberOfFiles validation:
  56. getNumberOfFiles: $.noop,
  57. // Error and info messages:
  58. messages: {
  59. maxNumberOfFiles: 'Maximum number of files exceeded',
  60. acceptFileTypes: 'File type not allowed',
  61. maxFileSize: 'File is too large',
  62. minFileSize: 'File is too small'
  63. }
  64. },
  65. processActions: {
  66. validate: function (data, options) {
  67. if (options.disabled) {
  68. return data;
  69. }
  70. // eslint-disable-next-line new-cap
  71. var dfd = $.Deferred(),
  72. settings = this.options,
  73. file = data.files[data.index],
  74. fileSize;
  75. if (options.minFileSize || options.maxFileSize) {
  76. fileSize = file.size;
  77. }
  78. if (
  79. $.type(options.maxNumberOfFiles) === 'number' &&
  80. (settings.getNumberOfFiles() || 0) + data.files.length >
  81. options.maxNumberOfFiles
  82. ) {
  83. file.error = settings.i18n('maxNumberOfFiles');
  84. } else if (
  85. options.acceptFileTypes &&
  86. !(
  87. options.acceptFileTypes.test(file.type) ||
  88. options.acceptFileTypes.test(file.name)
  89. )
  90. ) {
  91. file.error = settings.i18n('acceptFileTypes');
  92. } else if (fileSize > options.maxFileSize) {
  93. file.error = settings.i18n('maxFileSize');
  94. } else if (
  95. $.type(fileSize) === 'number' &&
  96. fileSize < options.minFileSize
  97. ) {
  98. file.error = settings.i18n('minFileSize');
  99. } else {
  100. delete file.error;
  101. }
  102. if (file.error || data.files.error) {
  103. data.files.error = true;
  104. dfd.rejectWith(this, [data]);
  105. } else {
  106. dfd.resolveWith(this, [data]);
  107. }
  108. return dfd.promise();
  109. }
  110. }
  111. });
  112. });