jquery.fileupload-validate.js 4.1 KB

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