3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('file-html5', function(Y) {
10 * The FileHTML5 class provides a wrapper for a file pointer in an HTML5 The File wrapper
11 * also implements the mechanics for uploading a file and tracking its progress.
16 * The class provides a wrapper for a file pointer.
20 * @param {Object} config Configuration object.
26 var FileHTML5 = function(o) {
30 if (FileHTML5.isValidFile(o)) {
33 else if (FileHTML5.isValidFile(o.file)) {
40 FileHTML5.superclass.constructor.apply(this, arguments);
42 if (file && FileHTML5.canUpload()) {
43 if (!this.get("file")) {
44 this._set("file", file);
47 if (!this.get("name")) {
48 this._set("name", file.name || file.fileName);
51 if (this.get("size") != (file.size || file.fileSize)) {
52 this._set("size", file.size || file.fileSize);
55 if (!this.get("type")) {
56 this._set("type", file.type);
59 if (file.hasOwnProperty("lastModifiedDate") && !this.get("dateModified")) {
60 this._set("dateModified", file.lastModifiedDate);
66 Y.extend(FileHTML5, Y.Base, {
69 * Construction logic executed during FileHTML5 instantiation.
74 initializer : function (cfg) {
75 if (!this.get("id")) {
76 this._set("id", Y.guid("file"));
81 * Handler of events dispatched by the XMLHTTPRequest.
83 * @method _uplodEventHandler
84 * @param {Event} event The event object received from the XMLHTTPRequest.
87 _uploadEventHandler: function (event) {
91 * Signals that progress has been made on the upload of this file.
93 * @event uploadprogress
94 * @param event {Event} The event object for the `uploadprogress` with the
97 * <dt>originEvent</dt>
98 * <dd>The original event fired by the XMLHttpRequest instance.</dd>
99 * <dt>bytesLoaded</dt>
100 * <dd>The number of bytes of the file that has been uploaded.</dd>
101 * <dt>bytesTotal</dt>
102 * <dd>The total number of bytes in the file (the file size)</dd>
103 * <dt>percentLoaded</dt>
104 * <dd>The fraction of the file that has been uploaded, out of 100.</dd>
107 this.fire("uploadprogress", {originEvent: event,
108 bytesLoaded: event.loaded,
109 bytesTotal: this.get("size"),
110 percentLoaded: Math.min(100, Math.round(10000*event.loaded/this.get("size"))/100)
112 this._set("bytesUploaded", event.loaded);
117 * Signals that this file's upload has completed and data has been received from the server.
119 * @event uploadcomplete
120 * @param event {Event} The event object for the `uploadcomplete` with the
123 * <dt>originEvent</dt>
124 * <dd>The original event fired by the XMLHttpRequest instance.</dd>
126 * <dd>The data returned by the server.</dd>
129 this.fire("uploadcomplete", {originEvent: event,
130 data: event.target.responseText});
131 var xhrupload = this.get("xhr").upload,
132 xhr = this.get("xhr"),
133 boundEventHandler = this.get("boundEventHandler");
135 xhrupload.removeEventListener ("progress", boundEventHandler);
136 xhrupload.removeEventListener ("error", boundEventHandler);
137 xhrupload.removeEventListener ("abort", boundEventHandler);
138 xhr.removeEventListener ("load", boundEventHandler);
139 xhr.removeEventListener ("readystatechange", boundEventHandler);
141 this._set("xhr", null);
145 var xhr = this.get("xhr");
147 * Signals that this file's upload has encountered an error.
150 * @param event {Event} The event object for the `uploaderror` with the
153 * <dt>originEvent</dt>
154 * <dd>The original event fired by the XMLHttpRequest instance.</dd>
156 * <dd>The status code reported by the XMLHttpRequest</dd>
157 * <dt>statusText</dt>
158 * <dd>The text of the error event reported by the XMLHttpRequest instance</dd>
161 this.fire("uploaderror", {originEvent: event,
163 statusText: xhr.statusText});
169 * Signals that this file's upload has been cancelled.
171 * @event uploadcancel
172 * @param event {Event} The event object for the `uploadcancel` with the
175 * <dt>originEvent</dt>
176 * <dd>The original event fired by the XMLHttpRequest instance.</dd>
179 this.fire("uploadcancel", {originEvent: event});
182 case "readystatechange":
185 * Signals that XMLHttpRequest has fired a readystatechange event.
187 * @event readystatechange
188 * @param event {Event} The event object for the `readystatechange` with the
191 * <dt>readyState</dt>
192 * <dd>The readyState code reported by the XMLHttpRequest instance.</dd>
193 * <dt>originEvent</dt>
194 * <dd>The original event fired by the XMLHttpRequest instance.</dd>
197 this.fire("readystatechange", {readyState: event.target.readyState,
198 originEvent: event});
204 * Starts the upload of a specific file.
206 * @method startUpload
207 * @param url {String} The URL to upload the file to.
208 * @param parameters {Object} (optional) A set of key-value pairs to send as variables along with the file upload HTTP request.
209 * @param fileFieldName {String} (optional) The name of the POST variable that should contain the uploaded file ('Filedata' by default)
211 startUpload: function(url, parameters, fileFieldName) {
213 this._set("bytesUploaded", 0);
215 this._set("xhr", new XMLHttpRequest());
216 this._set("boundEventHandler", Bind(this._uploadEventHandler, this));
218 var uploadData = new FormData(),
219 fileField = fileFieldName || "Filedata",
220 xhr = this.get("xhr"),
221 xhrupload = this.get("xhr").upload,
222 boundEventHandler = this.get("boundEventHandler");
224 Y.each(parameters, function (value, key) {uploadData.append(key, value);});
225 uploadData.append(fileField, this.get("file"));
227 xhrupload.addEventListener ("progress", boundEventHandler, false);
228 xhrupload.addEventListener ("error", boundEventHandler, false);
229 xhrupload.addEventListener ("abort", boundEventHandler, false);
231 xhr.addEventListener ("load", boundEventHandler, false);
232 xhr.addEventListener ("readystatechange", boundEventHandler, false);
235 xhr.open("POST", url, true);
236 xhr.send(uploadData);
239 * Signals that this file's upload has started.
242 * @param event {Event} The event object for the `uploadstart` with the
246 * <dd>The XMLHttpRequest instance handling the file upload.</dd>
249 this.fire("uploadstart", {xhr: xhr});
254 * Cancels the upload of a specific file, if currently in progress.
256 * @method cancelUpload
258 cancelUpload: function () {
259 this.get('xhr').abort();
266 * The identity of the class.
278 * The type of transport.
290 * Static property used to define the default attribute configuration of
301 * A String containing the unique id of the file wrapped by the FileFlash instance.
302 * The id is supplied by the Flash player uploader.
309 writeOnce: "initOnly",
314 * The size of the file wrapped by FileHTML5. This value is supplied by the instance of File().
321 writeOnce: "initOnly",
326 * The name of the file wrapped by FileHTML5. This value is supplied by the instance of File().
333 writeOnce: "initOnly",
338 * The date that the file wrapped by FileHTML5 was created on. This value is supplied by the instance of File().
340 * @attribute dateCreated
346 writeOnce: "initOnly",
351 * The date that the file wrapped by FileHTML5 was last modified on. This value is supplied by the instance of File().
353 * @attribute dateModified
358 writeOnce: "initOnly",
363 * The number of bytes of the file that has been uploaded to the server. This value is
364 * non-zero only while a file is being uploaded.
366 * @attribute bytesUploaded
376 * The type of the file wrapped by FileHTML. This value is provided by the instance of File()
383 writeOnce: "initOnly",
388 * The pointer to the instance of File() wrapped by FileHTML5.
395 writeOnce: "initOnly",
400 * The pointer to the instance of XMLHttpRequest used by FileHTML5 to upload the file.
403 * @type {XMLHttpRequest}
412 * The bound event handler used to handle events from XMLHttpRequest.
414 * @attribute boundEventHandler
425 * Checks whether a specific native file instance is valid
427 * @method isValidFile
428 * @param file {File} A native File() instance.
431 isValidFile: function (file) {
432 return (Win && Win.File && file instanceof File);
436 * Checks whether the browser has a native upload capability
437 * via XMLHttpRequest Level 2.
442 canUpload: function () {
443 return (Win && Win.FormData && Win.XMLHttpRequest);
447 Y.FileHTML5 = FileHTML5;
450 }, '3.5.1' ,{requires:['base']});