1
0

ariaNgFileService.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. (function () {
  2. 'use strict';
  3. angular.module('ariaNg').factory('ariaNgFileService', ['$window', function ($window) {
  4. var isSupportFileReader = !!$window.FileReader;
  5. var isSupportBlob = !!$window.Blob;
  6. var getAllowedExtensions = function (fileFilter) {
  7. var extensions = [];
  8. if (!fileFilter || fileFilter.length < 1) {
  9. extensions.push(/.+$/);
  10. return extensions;
  11. }
  12. var fileFilters = fileFilter.split(',');
  13. for (var i = 0; i < fileFilters.length; i++) {
  14. var extension = fileFilters[i];
  15. if (extension === '*.*') {
  16. extensions.push(/.+$/);
  17. continue;
  18. }
  19. extension = extension.replace('.', '\\.');
  20. extension = extension + '$';
  21. extensions.push(new RegExp(extension));
  22. }
  23. return extensions;
  24. };
  25. var checkFileExtension = function (fileName, extensions) {
  26. if (!extensions || extensions.length < 1) {
  27. return true;
  28. }
  29. for (var i = 0; i < extensions.length; i++) {
  30. if (extensions[i].test(fileName)) {
  31. return true;
  32. }
  33. }
  34. return false;
  35. };
  36. return {
  37. isSupportFileReader: function () {
  38. return isSupportFileReader;
  39. },
  40. isSupportBlob: function () {
  41. return isSupportBlob;
  42. },
  43. openFileContent: function (options, successCallback, errorCallback, element) {
  44. if (!isSupportFileReader) {
  45. if (errorCallback) {
  46. errorCallback('Your browser does not support loading file!');
  47. }
  48. return;
  49. }
  50. options = angular.extend({
  51. scope: null,
  52. fileFilter: null,
  53. fileType: 'binary', // or 'text'
  54. successCallback: successCallback,
  55. errorCallback: errorCallback
  56. }, options);
  57. if (!element || !element.change) {
  58. element = angular.element('<input type="file" style="display: none"/>');
  59. }
  60. element.data('options', options);
  61. if (options.fileFilter) {
  62. element.attr('accept', options.fileFilter);
  63. }
  64. element.val('');
  65. if (element.attr('data-ariang-file-initialized') !== 'true') {
  66. element.change(function () {
  67. if (!this.files || this.files.length < 1) {
  68. return;
  69. }
  70. var thisOptions = element.data('options');
  71. var allowedExtensions = getAllowedExtensions(thisOptions.fileFilter);
  72. var file = this.files[0];
  73. var fileName = file.name;
  74. if (!checkFileExtension(fileName, allowedExtensions)) {
  75. if (thisOptions.errorCallback) {
  76. if (thisOptions.scope) {
  77. thisOptions.scope.$apply(function () {
  78. thisOptions.errorCallback('The selected file type is invalid!');
  79. });
  80. } else {
  81. thisOptions.errorCallback('The selected file type is invalid!');
  82. }
  83. }
  84. return;
  85. }
  86. var reader = new FileReader();
  87. reader.onload = function () {
  88. var result = {
  89. fileName: fileName
  90. };
  91. switch (thisOptions.fileType) {
  92. case 'text':
  93. result.content = this.result;
  94. break;
  95. case 'binary':
  96. default:
  97. result.base64Content = this.result.replace(/.*?base64,/, '');
  98. break;
  99. }
  100. if (thisOptions.successCallback) {
  101. if (thisOptions.scope) {
  102. thisOptions.scope.$apply(function () {
  103. thisOptions.successCallback(result);
  104. });
  105. } else {
  106. thisOptions.successCallback(result);
  107. }
  108. }
  109. };
  110. reader.onerror = function () {
  111. if (thisOptions.errorCallback) {
  112. if (thisOptions.scope) {
  113. thisOptions.scope.$apply(function () {
  114. thisOptions.errorCallback('Failed to load file!');
  115. });
  116. } else {
  117. thisOptions.errorCallback('Failed to load file!');
  118. }
  119. }
  120. };
  121. switch (thisOptions.fileType) {
  122. case 'text':
  123. reader.readAsText(file);
  124. break;
  125. case 'binary':
  126. default:
  127. reader.readAsDataURL(file);
  128. break;
  129. }
  130. }).attr('data-ariang-file-initialized', 'true');
  131. }
  132. element.trigger('click');
  133. },
  134. saveFileContent: function (content, element, options) {
  135. if (!isSupportBlob) {
  136. return;
  137. }
  138. options = angular.extend({
  139. fileName: null,
  140. contentType: 'application/octet-stream',
  141. autoTrigger: false,
  142. autoRevoke: false
  143. }, options);
  144. var blob = new Blob([content], { type: options.contentType });
  145. var objectUrl = URL.createObjectURL(blob);
  146. if (!element) {
  147. element = angular.element('<a style="display: none"/>');
  148. }
  149. element.attr('href', objectUrl);
  150. if (options.fileName) {
  151. element.attr('download', options.fileName);
  152. }
  153. if (options.autoTrigger) {
  154. element.trigger('click');
  155. }
  156. if (options.autoRevoke) {
  157. URL.revokeObjectURL(objectUrl);
  158. }
  159. }
  160. };
  161. }]);
  162. }());