2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('file-html5', function (Y, NAME) {
11 * The FileHTML5 class provides a wrapper for a file pointer in an HTML5 The File wrapper
12 * also implements the mechanics for uploading a file and tracking its progress.
17 * The class provides a wrapper for a file pointer.
21 * @param {Object} config Configuration object.
27 var FileHTML5 = function(o) {
31 if (FileHTML5.isValidFile(o)) {
34 else if (FileHTML5.isValidFile(o.file)) {
41 FileHTML5.superclass.constructor.apply(this, arguments);
43 if (file && FileHTML5.canUpload()) {
44 if (!this.get("file")) {
45 this._set("file", file);
48 if (!this.get("name")) {
49 this._set("name", file.name || file.fileName);
52 if (this.get("size") != (file.size || file.fileSize)) {
53 this._set("size", file.size || file.fileSize);
56 if (!this.get("type")) {
57 this._set("type", file.type);
60 if (file.hasOwnProperty("lastModifiedDate") && !this.get("dateModified")) {
61 this._set("dateModified", file.lastModifiedDate);
67 Y.extend(FileHTML5, Y.Base, {
70 * Construction logic executed during FileHTML5 instantiation.
75 initializer : function (cfg) {
76 if (!this.get("id")) {
77 this._set("id", Y.guid("file"));
82 * Handler of events dispatched by the XMLHTTPRequest.
84 * @method _uploadEventHandler
85 * @param {Event} event The event object received from the XMLHTTPRequest.
88 _uploadEventHandler: function (event) {
89 var xhr = this.get("xhr");
94 * Signals that progress has been made on the upload of this file.
96 * @event uploadprogress
97 * @param event {Event} The event object for the `uploadprogress` with the
100 * <dt>originEvent</dt>
101 * <dd>The original event fired by the XMLHttpRequest instance.</dd>
102 * <dt>bytesLoaded</dt>
103 * <dd>The number of bytes of the file that has been uploaded.</dd>
104 * <dt>bytesTotal</dt>
105 * <dd>The total number of bytes in the file (the file size)</dd>
106 * <dt>percentLoaded</dt>
107 * <dd>The fraction of the file that has been uploaded, out of 100.</dd>
110 this.fire("uploadprogress", {originEvent: event,
111 bytesLoaded: event.loaded,
112 bytesTotal: this.get("size"),
113 percentLoaded: Math.min(100, Math.round(10000*event.loaded/this.get("size"))/100)
115 this._set("bytesUploaded", event.loaded);
120 * Signals that this file's upload has completed and data has been received from the server.
122 * @event uploadcomplete
123 * @param event {Event} The event object for the `uploadcomplete` with the
126 * <dt>originEvent</dt>
127 * <dd>The original event fired by the XMLHttpRequest instance.</dd>
129 * <dd>The data returned by the server.</dd>
133 if (xhr.status >= 200 && xhr.status <= 299) {
134 this.fire("uploadcomplete", {originEvent: event,
135 data: event.target.responseText});
136 var xhrupload = xhr.upload,
137 boundEventHandler = this.get("boundEventHandler");
139 xhrupload.removeEventListener ("progress", boundEventHandler);
140 xhrupload.removeEventListener ("error", boundEventHandler);
141 xhrupload.removeEventListener ("abort", boundEventHandler);
142 xhr.removeEventListener ("load", boundEventHandler);
143 xhr.removeEventListener ("error", boundEventHandler);
144 xhr.removeEventListener ("readystatechange", boundEventHandler);
146 this._set("xhr", null);
149 this.fire("uploaderror", {originEvent: event,
151 statusText: xhr.statusText,
158 * Signals that this file's upload has encountered an error.
161 * @param event {Event} The event object for the `uploaderror` with the
164 * <dt>originEvent</dt>
165 * <dd>The original event fired by the XMLHttpRequest instance.</dd>
167 * <dd>The status code reported by the XMLHttpRequest. If it's an HTTP error,
168 then this corresponds to the HTTP status code received by the uploader.</dd>
169 * <dt>statusText</dt>
170 * <dd>The text of the error event reported by the XMLHttpRequest instance</dd>
172 * <dd>Either "http" (if it's an HTTP error), or "io" (if it's a network transmission
177 this.fire("uploaderror", {originEvent: event,
179 statusText: xhr.statusText,
186 * Signals that this file's upload has been cancelled.
188 * @event uploadcancel
189 * @param event {Event} The event object for the `uploadcancel` with the
192 * <dt>originEvent</dt>
193 * <dd>The original event fired by the XMLHttpRequest instance.</dd>
196 this.fire("uploadcancel", {originEvent: event});
199 case "readystatechange":
202 * Signals that XMLHttpRequest has fired a readystatechange event.
204 * @event readystatechange
205 * @param event {Event} The event object for the `readystatechange` with the
208 * <dt>readyState</dt>
209 * <dd>The readyState code reported by the XMLHttpRequest instance.</dd>
210 * <dt>originEvent</dt>
211 * <dd>The original event fired by the XMLHttpRequest instance.</dd>
214 this.fire("readystatechange", {readyState: event.target.readyState,
215 originEvent: event});
221 * Starts the upload of a specific file.
223 * @method startUpload
224 * @param url {String} The URL to upload the file to.
225 * @param parameters {Object} (optional) A set of key-value pairs to send as variables along with the file upload HTTP request.
226 * @param fileFieldName {String} (optional) The name of the POST variable that should contain the uploaded file ('Filedata' by default)
228 startUpload: function(url, parameters, fileFieldName) {
230 this._set("bytesUploaded", 0);
232 this._set("xhr", new XMLHttpRequest());
233 this._set("boundEventHandler", Bind(this._uploadEventHandler, this));
235 var uploadData = new FormData(),
236 fileField = fileFieldName || "Filedata",
237 xhr = this.get("xhr"),
238 xhrupload = this.get("xhr").upload,
239 boundEventHandler = this.get("boundEventHandler");
241 Y.each(parameters, function (value, key) {uploadData.append(key, value);});
242 uploadData.append(fileField, this.get("file"));
247 xhr.addEventListener ("loadstart", boundEventHandler, false);
248 xhrupload.addEventListener ("progress", boundEventHandler, false);
249 xhr.addEventListener ("load", boundEventHandler, false);
250 xhr.addEventListener ("error", boundEventHandler, false);
251 xhrupload.addEventListener ("error", boundEventHandler, false);
252 xhrupload.addEventListener ("abort", boundEventHandler, false);
253 xhr.addEventListener ("abort", boundEventHandler, false);
254 xhr.addEventListener ("loadend", boundEventHandler, false);
255 xhr.addEventListener ("readystatechange", boundEventHandler, false);
257 xhr.open("POST", url, true);
259 xhr.withCredentials = this.get("xhrWithCredentials");
261 Y.each(this.get("xhrHeaders"), function (value, key) {
262 xhr.setRequestHeader(key, value);
265 xhr.send(uploadData);
268 * Signals that this file's upload has started.
271 * @param event {Event} The event object for the `uploadstart` with the
275 * <dd>The XMLHttpRequest instance handling the file upload.</dd>
278 this.fire("uploadstart", {xhr: xhr});
283 * Cancels the upload of a specific file, if currently in progress.
285 * @method cancelUpload
287 cancelUpload: function () {
288 var xhr = this.get('xhr');
298 * The identity of the class.
310 * The type of transport.
322 * Static property used to define the default attribute configuration of
333 * A String containing the unique id of the file wrapped by the FileFlash instance.
334 * The id is supplied by the Flash player uploader.
341 writeOnce: "initOnly",
346 * The size of the file wrapped by FileHTML5. This value is supplied by the instance of File().
353 writeOnce: "initOnly",
358 * The name of the file wrapped by FileHTML5. This value is supplied by the instance of File().
365 writeOnce: "initOnly",
370 * The date that the file wrapped by FileHTML5 was created on. This value is supplied by the instance of File().
372 * @attribute dateCreated
378 writeOnce: "initOnly",
383 * The date that the file wrapped by FileHTML5 was last modified on. This value is supplied by the instance of File().
385 * @attribute dateModified
390 writeOnce: "initOnly",
395 * The number of bytes of the file that has been uploaded to the server. This value is
396 * non-zero only while a file is being uploaded.
398 * @attribute bytesUploaded
408 * The type of the file wrapped by FileHTML. This value is provided by the instance of File()
415 writeOnce: "initOnly",
420 * The pointer to the instance of File() wrapped by FileHTML5.
427 writeOnce: "initOnly",
432 * The pointer to the instance of XMLHttpRequest used by FileHTML5 to upload the file.
435 * @type {XMLHttpRequest}
444 * The dictionary of headers that should be set on the XMLHttpRequest object before
447 * @attribute xhrHeaders
456 * A Boolean indicating whether the XMLHttpRequest should be sent with user credentials.
457 * This does not affect same-site requests.
459 * @attribute xhrWithCredentials
463 xhrWithCredentials: {
468 * The bound event handler used to handle events from XMLHttpRequest.
470 * @attribute boundEventHandler
481 * Checks whether a specific native file instance is valid
483 * @method isValidFile
484 * @param file {File} A native File() instance.
487 isValidFile: function (file) {
488 return (Win && Win.File && file instanceof File);
492 * Checks whether the browser has a native upload capability
493 * via XMLHttpRequest Level 2.
498 canUpload: function () {
499 return (Win && Win.FormData && Win.XMLHttpRequest);
503 Y.FileHTML5 = FileHTML5;
506 }, '3.13.0', {"requires": ["base"]});