jquery.fileupload-validate.js 4.0 KB

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