jquery.fileupload-validate.js 4.1 KB

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