filereader.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // Copyright 2011 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview A wrapper for the HTML5 FileReader object.
  16. *
  17. */
  18. goog.provide('goog.fs.FileReader');
  19. goog.provide('goog.fs.FileReader.EventType');
  20. goog.provide('goog.fs.FileReader.ReadyState');
  21. goog.require('goog.async.Deferred');
  22. goog.require('goog.events.EventTarget');
  23. goog.require('goog.fs.Error');
  24. goog.require('goog.fs.ProgressEvent');
  25. /**
  26. * An object for monitoring the reading of files. This emits ProgressEvents of
  27. * the types listed in {@link goog.fs.FileReader.EventType}.
  28. *
  29. * @constructor
  30. * @extends {goog.events.EventTarget}
  31. * @final
  32. */
  33. goog.fs.FileReader = function() {
  34. goog.fs.FileReader.base(this, 'constructor');
  35. /**
  36. * The underlying FileReader object.
  37. *
  38. * @type {!FileReader}
  39. * @private
  40. */
  41. this.reader_ = new FileReader();
  42. this.reader_.onloadstart = goog.bind(this.dispatchProgressEvent_, this);
  43. this.reader_.onprogress = goog.bind(this.dispatchProgressEvent_, this);
  44. this.reader_.onload = goog.bind(this.dispatchProgressEvent_, this);
  45. this.reader_.onabort = goog.bind(this.dispatchProgressEvent_, this);
  46. this.reader_.onerror = goog.bind(this.dispatchProgressEvent_, this);
  47. this.reader_.onloadend = goog.bind(this.dispatchProgressEvent_, this);
  48. };
  49. goog.inherits(goog.fs.FileReader, goog.events.EventTarget);
  50. /**
  51. * Possible states for a FileReader.
  52. *
  53. * @enum {number}
  54. */
  55. goog.fs.FileReader.ReadyState = {
  56. /**
  57. * The object has been constructed, but there is no pending read.
  58. */
  59. INIT: 0,
  60. /**
  61. * Data is being read.
  62. */
  63. LOADING: 1,
  64. /**
  65. * The data has been read from the file, the read was aborted, or an error
  66. * occurred.
  67. */
  68. DONE: 2
  69. };
  70. /**
  71. * Events emitted by a FileReader.
  72. *
  73. * @enum {string}
  74. */
  75. goog.fs.FileReader.EventType = {
  76. /**
  77. * Emitted when the reading begins. readyState will be LOADING.
  78. */
  79. LOAD_START: 'loadstart',
  80. /**
  81. * Emitted when progress has been made in reading the file. readyState will be
  82. * LOADING.
  83. */
  84. PROGRESS: 'progress',
  85. /**
  86. * Emitted when the data has been successfully read. readyState will be
  87. * LOADING.
  88. */
  89. LOAD: 'load',
  90. /**
  91. * Emitted when the reading has been aborted. readyState will be LOADING.
  92. */
  93. ABORT: 'abort',
  94. /**
  95. * Emitted when an error is encountered or the reading has been aborted.
  96. * readyState will be LOADING.
  97. */
  98. ERROR: 'error',
  99. /**
  100. * Emitted when the reading is finished, whether successfully or not.
  101. * readyState will be DONE.
  102. */
  103. LOAD_END: 'loadend'
  104. };
  105. /**
  106. * Abort the reading of the file.
  107. */
  108. goog.fs.FileReader.prototype.abort = function() {
  109. try {
  110. this.reader_.abort();
  111. } catch (e) {
  112. throw new goog.fs.Error(e, 'aborting read');
  113. }
  114. };
  115. /**
  116. * @return {goog.fs.FileReader.ReadyState} The current state of the FileReader.
  117. */
  118. goog.fs.FileReader.prototype.getReadyState = function() {
  119. return /** @type {goog.fs.FileReader.ReadyState} */ (this.reader_.readyState);
  120. };
  121. /**
  122. * @return {*} The result of the file read.
  123. */
  124. goog.fs.FileReader.prototype.getResult = function() {
  125. return this.reader_.result;
  126. };
  127. /**
  128. * @return {goog.fs.Error} The error encountered while reading, if any.
  129. */
  130. goog.fs.FileReader.prototype.getError = function() {
  131. return this.reader_.error &&
  132. new goog.fs.Error(this.reader_.error, 'reading file');
  133. };
  134. /**
  135. * Wrap a progress event emitted by the underlying file reader and re-emit it.
  136. *
  137. * @param {!ProgressEvent} event The underlying event.
  138. * @private
  139. */
  140. goog.fs.FileReader.prototype.dispatchProgressEvent_ = function(event) {
  141. this.dispatchEvent(new goog.fs.ProgressEvent(event, this));
  142. };
  143. /** @override */
  144. goog.fs.FileReader.prototype.disposeInternal = function() {
  145. goog.fs.FileReader.base(this, 'disposeInternal');
  146. delete this.reader_;
  147. };
  148. /**
  149. * Starts reading a blob as a binary string.
  150. * @param {!Blob} blob The blob to read.
  151. */
  152. goog.fs.FileReader.prototype.readAsBinaryString = function(blob) {
  153. this.reader_.readAsBinaryString(blob);
  154. };
  155. /**
  156. * Reads a blob as a binary string.
  157. * @param {!Blob} blob The blob to read.
  158. * @return {!goog.async.Deferred} The deferred Blob contents as a binary string.
  159. * If an error occurs, the errback is called with a {@link goog.fs.Error}.
  160. */
  161. goog.fs.FileReader.readAsBinaryString = function(blob) {
  162. var reader = new goog.fs.FileReader();
  163. var d = goog.fs.FileReader.createDeferred_(reader);
  164. reader.readAsBinaryString(blob);
  165. return d;
  166. };
  167. /**
  168. * Starts reading a blob as an array buffer.
  169. * @param {!Blob} blob The blob to read.
  170. */
  171. goog.fs.FileReader.prototype.readAsArrayBuffer = function(blob) {
  172. this.reader_.readAsArrayBuffer(blob);
  173. };
  174. /**
  175. * Reads a blob as an array buffer.
  176. * @param {!Blob} blob The blob to read.
  177. * @return {!goog.async.Deferred} The deferred Blob contents as an array buffer.
  178. * If an error occurs, the errback is called with a {@link goog.fs.Error}.
  179. */
  180. goog.fs.FileReader.readAsArrayBuffer = function(blob) {
  181. var reader = new goog.fs.FileReader();
  182. var d = goog.fs.FileReader.createDeferred_(reader);
  183. reader.readAsArrayBuffer(blob);
  184. return d;
  185. };
  186. /**
  187. * Starts reading a blob as text.
  188. * @param {!Blob} blob The blob to read.
  189. * @param {string=} opt_encoding The name of the encoding to use.
  190. */
  191. goog.fs.FileReader.prototype.readAsText = function(blob, opt_encoding) {
  192. this.reader_.readAsText(blob, opt_encoding);
  193. };
  194. /**
  195. * Reads a blob as text.
  196. * @param {!Blob} blob The blob to read.
  197. * @param {string=} opt_encoding The name of the encoding to use.
  198. * @return {!goog.async.Deferred} The deferred Blob contents as text.
  199. * If an error occurs, the errback is called with a {@link goog.fs.Error}.
  200. */
  201. goog.fs.FileReader.readAsText = function(blob, opt_encoding) {
  202. var reader = new goog.fs.FileReader();
  203. var d = goog.fs.FileReader.createDeferred_(reader);
  204. reader.readAsText(blob, opt_encoding);
  205. return d;
  206. };
  207. /**
  208. * Starts reading a blob as a data URL.
  209. * @param {!Blob} blob The blob to read.
  210. */
  211. goog.fs.FileReader.prototype.readAsDataUrl = function(blob) {
  212. this.reader_.readAsDataURL(blob);
  213. };
  214. /**
  215. * Reads a blob as a data URL.
  216. * @param {!Blob} blob The blob to read.
  217. * @return {!goog.async.Deferred} The deferred Blob contents as a data URL.
  218. * If an error occurs, the errback is called with a {@link goog.fs.Error}.
  219. */
  220. goog.fs.FileReader.readAsDataUrl = function(blob) {
  221. var reader = new goog.fs.FileReader();
  222. var d = goog.fs.FileReader.createDeferred_(reader);
  223. reader.readAsDataUrl(blob);
  224. return d;
  225. };
  226. /**
  227. * Creates a new deferred object for the results of a read method.
  228. * @param {goog.fs.FileReader} reader The reader to create a deferred for.
  229. * @return {!goog.async.Deferred} The deferred results.
  230. * @private
  231. */
  232. goog.fs.FileReader.createDeferred_ = function(reader) {
  233. var deferred = new goog.async.Deferred();
  234. reader.listen(
  235. goog.fs.FileReader.EventType.LOAD_END, goog.partial(function(d, r, e) {
  236. var result = r.getResult();
  237. var error = r.getError();
  238. if (result != null && !error) {
  239. d.callback(result);
  240. } else {
  241. d.errback(error);
  242. }
  243. r.dispose();
  244. }, deferred, reader));
  245. return deferred;
  246. };