PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/1.3b/j/swfupload-2.1.0b2/swfupload.js

http://kfm.googlecode.com/
JavaScript | 725 lines | 448 code | 118 blank | 159 comment | 59 complexity | ab75e08c2c3e661de0f58b9f7e10ada8 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, Apache-2.0
  1. /**
  2. * SWFUpload v2.1.0 by Jacob Roberts, Feb 2008, http://www.swfupload.org, http://swfupload.googlecode.com, http://www.swfupload.org
  3. * -------- -------- -------- -------- -------- -------- -------- --------
  4. * SWFUpload is (c) 2006 Lars Huring, Olov Nilzén and Mammon Media and is released under the MIT License:
  5. * http://www.opensource.org/licenses/mit-license.php
  6. *
  7. * See Changelog.txt for version history
  8. *
  9. */
  10. /* *********** */
  11. /* Constructor */
  12. /* *********** */
  13. var SWFUpload = function (settings) {
  14. this.initSWFUpload(settings);
  15. };
  16. SWFUpload.prototype.initSWFUpload = function (settings) {
  17. try {
  18. this.customSettings = {}; // A container where developers can place their own settings associated with this instance.
  19. this.settings = settings;
  20. this.eventQueue = [];
  21. this.movieName = "SWFUpload_" + SWFUpload.movieCount++;
  22. this.movieElement = null;
  23. // Setup global control tracking
  24. SWFUpload.instances[this.movieName] = this;
  25. // Load the settings. Load the Flash movie.
  26. this.initSettings();
  27. this.loadFlash();
  28. this.displayDebugInfo();
  29. } catch (ex) {
  30. delete SWFUpload.instances[this.movieName];
  31. throw ex;
  32. }
  33. };
  34. /* *************** */
  35. /* Static Members */
  36. /* *************** */
  37. SWFUpload.instances = {};
  38. SWFUpload.movieCount = 0;
  39. SWFUpload.version = "2.1.0 beta 1";
  40. SWFUpload.QUEUE_ERROR = {
  41. QUEUE_LIMIT_EXCEEDED : -100,
  42. FILE_EXCEEDS_SIZE_LIMIT : -110,
  43. ZERO_BYTE_FILE : -120,
  44. INVALID_FILETYPE : -130
  45. };
  46. SWFUpload.UPLOAD_ERROR = {
  47. HTTP_ERROR : -200,
  48. MISSING_UPLOAD_URL : -210,
  49. IO_ERROR : -220,
  50. SECURITY_ERROR : -230,
  51. UPLOAD_LIMIT_EXCEEDED : -240,
  52. UPLOAD_FAILED : -250,
  53. SPECIFIED_FILE_ID_NOT_FOUND : -260,
  54. FILE_VALIDATION_FAILED : -270,
  55. FILE_CANCELLED : -280,
  56. UPLOAD_STOPPED : -290
  57. };
  58. SWFUpload.FILE_STATUS = {
  59. QUEUED : -1,
  60. IN_PROGRESS : -2,
  61. ERROR : -3,
  62. COMPLETE : -4,
  63. CANCELLED : -5
  64. };
  65. /* ******************** */
  66. /* Instance Members */
  67. /* ******************** */
  68. // Private: initSettings ensures that all the
  69. // settings are set, getting a default value if one was not assigned.
  70. SWFUpload.prototype.initSettings = function () {
  71. this.ensureDefault = function (settingName, defaultValue) {
  72. this.settings[settingName] = (this.settings[settingName] == undefined) ? defaultValue : this.settings[settingName];
  73. };
  74. // Upload backend settings
  75. this.ensureDefault("upload_url", "");
  76. this.ensureDefault("file_post_name", "Filedata");
  77. this.ensureDefault("post_params", {});
  78. this.ensureDefault("use_query_string", false);
  79. this.ensureDefault("requeue_on_error", false);
  80. // File Settings
  81. this.ensureDefault("file_types", "*.*");
  82. this.ensureDefault("file_types_description", "All Files");
  83. this.ensureDefault("file_size_limit", 0); // Default zero means "unlimited"
  84. this.ensureDefault("file_upload_limit", 0);
  85. this.ensureDefault("file_queue_limit", 0);
  86. // Flash Settings
  87. this.ensureDefault("flash_url", "swfupload_f9.swf");
  88. this.ensureDefault("flash_color", "#FFFFFF");
  89. // Debug Settings
  90. this.ensureDefault("debug", false);
  91. this.settings.debug_enabled = this.settings.debug; // Here to maintain v2 API
  92. // Event Handlers
  93. this.settings.return_upload_start_handler = this.returnUploadStart;
  94. this.ensureDefault("swfupload_loaded_handler", null);
  95. this.ensureDefault("file_dialog_start_handler", null);
  96. this.ensureDefault("file_queued_handler", null);
  97. this.ensureDefault("file_queue_error_handler", null);
  98. this.ensureDefault("file_dialog_complete_handler", null);
  99. this.ensureDefault("upload_start_handler", null);
  100. this.ensureDefault("upload_progress_handler", null);
  101. this.ensureDefault("upload_error_handler", null);
  102. this.ensureDefault("upload_success_handler", null);
  103. this.ensureDefault("upload_complete_handler", null);
  104. this.ensureDefault("debug_handler", this.debugMessage);
  105. this.ensureDefault("custom_settings", {});
  106. // Other settings
  107. this.customSettings = this.settings.custom_settings;
  108. delete this.ensureDefault;
  109. };
  110. // Private: loadFlash generates the HTML tag for the Flash
  111. // It then adds the flash to the body
  112. SWFUpload.prototype.loadFlash = function () {
  113. var targetElement, container;
  114. // Make sure an element with the ID we are going to use doesn't already exist
  115. if (document.getElementById(this.movieName) !== null) {
  116. throw "ID " + this.movieName + " is already in use. The Flash Object could not be added";
  117. }
  118. // Get the body tag where we will be adding the flash movie
  119. targetElement = document.getElementsByTagName("body")[0];
  120. if (targetElement == undefined) {
  121. throw "Could not find the 'body' element.";
  122. }
  123. // Append the container and load the flash
  124. container = document.createElement("div");
  125. container.style.width = "1px";
  126. container.style.height = "1px";
  127. targetElement.appendChild(container);
  128. container.innerHTML = this.getFlashHTML(); // Using innerHTML is non-standard but the only sensible way to dynamically add Flash in IE (and maybe other browsers)
  129. };
  130. // Private: getFlashHTML generates the object tag needed to embed the flash in to the document
  131. SWFUpload.prototype.getFlashHTML = function () {
  132. // Flash Satay object syntax: http://www.alistapart.com/articles/flashsatay
  133. return ['<object id="', this.movieName, '" type="application/x-shockwave-flash" data="', this.settings.flash_url, '" width="1" height="1" style="-moz-user-focus: ignore;">',
  134. '<param name="movie" value="', this.settings.flash_url, '" />',
  135. '<param name="bgcolor" value="', this.settings.flash_color, '" />',
  136. '<param name="quality" value="high" />',
  137. '<param name="menu" value="false" />',
  138. '<param name="allowScriptAccess" value="always" />',
  139. '<param name="flashvars" value="' + this.getFlashVars() + '" />',
  140. '</object>'].join("");
  141. };
  142. // Private: getFlashVars builds the parameter string that will be passed
  143. // to flash in the flashvars param.
  144. SWFUpload.prototype.getFlashVars = function () {
  145. // Build a string from the post param object
  146. var paramString = this.buildParamString();
  147. // Build the parameter string
  148. return ["movieName=", encodeURIComponent(this.movieName),
  149. "&amp;uploadURL=", encodeURIComponent(this.settings.upload_url),
  150. "&amp;useQueryString=", encodeURIComponent(this.settings.use_query_string),
  151. "&amp;requeueOnError=", encodeURIComponent(this.settings.requeue_on_error),
  152. "&amp;params=", encodeURIComponent(paramString),
  153. "&amp;filePostName=", encodeURIComponent(this.settings.file_post_name),
  154. "&amp;fileTypes=", encodeURIComponent(this.settings.file_types),
  155. "&amp;fileTypesDescription=", encodeURIComponent(this.settings.file_types_description),
  156. "&amp;fileSizeLimit=", encodeURIComponent(this.settings.file_size_limit),
  157. "&amp;fileUploadLimit=", encodeURIComponent(this.settings.file_upload_limit),
  158. "&amp;fileQueueLimit=", encodeURIComponent(this.settings.file_queue_limit),
  159. "&amp;debugEnabled=", encodeURIComponent(this.settings.debug_enabled)].join("");
  160. };
  161. // Public: getMovieElement retrieves the DOM reference to the Flash element added by SWFUpload
  162. // The element is cached after the first lookup
  163. SWFUpload.prototype.getMovieElement = function () {
  164. if (this.movieElement == undefined) {
  165. this.movieElement = document.getElementById(this.movieName);
  166. }
  167. if (this.movieElement === null) {
  168. throw "Could not find Flash element";
  169. }
  170. return this.movieElement;
  171. };
  172. // Private: buildParamString takes the name/value pairs in the post_params setting object
  173. // and joins them up in to a string formatted "name=value&amp;name=value"
  174. SWFUpload.prototype.buildParamString = function () {
  175. var postParams = this.settings.post_params;
  176. var paramStringPairs = [];
  177. if (typeof(postParams) === "object") {
  178. for (var name in postParams) {
  179. if (postParams.hasOwnProperty(name)) {
  180. paramStringPairs.push(encodeURIComponent(name.toString()) + "=" + encodeURIComponent(postParams[name].toString()));
  181. }
  182. }
  183. }
  184. return paramStringPairs.join("&amp;");
  185. };
  186. // Public: displayDebugInfo prints out settings and configuration
  187. // information about this SWFUpload instance.
  188. // This function (and any references to it) can be deleted when placing
  189. // SWFUpload in production.
  190. SWFUpload.prototype.displayDebugInfo = function () {
  191. this.debug(
  192. [
  193. "---SWFUpload Instance Info---\n",
  194. "Version: ", SWFUpload.version, "\n",
  195. "Movie Name: ", this.movieName, "\n",
  196. "Settings:\n",
  197. "\t", "upload_url: ", this.settings.upload_url, "\n",
  198. "\t", "use_query_string: ", this.settings.use_query_string.toString(), "\n",
  199. "\t", "file_post_name: ", this.settings.file_post_name, "\n",
  200. "\t", "post_params: ", this.settings.post_params.toString(), "\n",
  201. "\t", "file_types: ", this.settings.file_types, "\n",
  202. "\t", "file_types_description: ", this.settings.file_types_description, "\n",
  203. "\t", "file_size_limit: ", this.settings.file_size_limit, "\n",
  204. "\t", "file_upload_limit: ", this.settings.file_upload_limit, "\n",
  205. "\t", "file_queue_limit: ", this.settings.file_queue_limit, "\n",
  206. "\t", "flash_url: ", this.settings.flash_url, "\n",
  207. "\t", "flash_color: ", this.settings.flash_color, "\n",
  208. "\t", "debug: ", this.settings.debug.toString(), "\n",
  209. "\t", "custom_settings: ", this.settings.custom_settings.toString(), "\n",
  210. "Event Handlers:\n",
  211. "\t", "swfupload_loaded_handler assigned: ", (typeof(this.settings.swfupload_loaded_handler) === "function").toString(), "\n",
  212. "\t", "file_dialog_start_handler assigned: ", (typeof(this.settings.file_dialog_start_handler) === "function").toString(), "\n",
  213. "\t", "file_queued_handler assigned: ", (typeof(this.settings.file_queued_handler) === "function").toString(), "\n",
  214. "\t", "file_queue_error_handler assigned: ", (typeof(this.settings.file_queue_error_handler) === "function").toString(), "\n",
  215. "\t", "upload_start_handler assigned: ", (typeof(this.settings.upload_start_handler) === "function").toString(), "\n",
  216. "\t", "upload_progress_handler assigned: ", (typeof(this.settings.upload_progress_handler) === "function").toString(), "\n",
  217. "\t", "upload_error_handler assigned: ", (typeof(this.settings.upload_error_handler) === "function").toString(), "\n",
  218. "\t", "upload_success_handler assigned: ", (typeof(this.settings.upload_success_handler) === "function").toString(), "\n",
  219. "\t", "upload_complete_handler assigned: ", (typeof(this.settings.upload_complete_handler) === "function").toString(), "\n",
  220. "\t", "debug_handler assigned: ", (typeof(this.settings.debug_handler) === "function").toString(), "\n"
  221. ].join("")
  222. );
  223. };
  224. /* Note: addSetting and getSetting are no longer used by SWFUpload but are included
  225. the maintain v2 API compatibility
  226. */
  227. // Public: (Deprecated) addSetting adds a setting value. If the value given is undefined or null then the default_value is used.
  228. SWFUpload.prototype.addSetting = function (name, value, default_value) {
  229. if (value == undefined) {
  230. return (this.settings[name] = default_value);
  231. } else {
  232. return (this.settings[name] = value);
  233. }
  234. };
  235. // Public: (Deprecated) getSetting gets a setting. Returns an empty string if the setting was not found.
  236. SWFUpload.prototype.getSetting = function (name) {
  237. if (this.settings[name] != undefined) {
  238. return this.settings[name];
  239. }
  240. return "";
  241. };
  242. // Private: callFlash handles function calls made to the Flash element.
  243. // Calls are made with a setTimeout for some functions to work around
  244. // bugs in the ExternalInterface library.
  245. // NOTE: if we don't need to call StartUpload with a timeout anymore then we can simplify this
  246. // function and remove all the withTimeout stuff
  247. SWFUpload.prototype.callFlash = function (functionName, withTimeout, argumentArray) {
  248. withTimeout = !!withTimeout || false;
  249. argumentArray = argumentArray || [];
  250. var self = this;
  251. var callFunction = function () {
  252. var movieElement = self.getMovieElement();
  253. var returnValue;
  254. if (typeof(movieElement[functionName]) === "function") {
  255. // We have to go through all this if/else stuff because the Flash functions don't have apply() and only accept the exact number of arguments.
  256. if (argumentArray.length === 0) {
  257. returnValue = movieElement[functionName]();
  258. } else if (argumentArray.length === 1) {
  259. returnValue = movieElement[functionName](argumentArray[0]);
  260. } else if (argumentArray.length === 2) {
  261. returnValue = movieElement[functionName](argumentArray[0], argumentArray[1]);
  262. } else if (argumentArray.length === 3) {
  263. returnValue = movieElement[functionName](argumentArray[0], argumentArray[1], argumentArray[2]);
  264. } else {
  265. throw "Too many arguments";
  266. }
  267. // Unescape file post param values
  268. if (returnValue != undefined && typeof(returnValue.post) === "object") {
  269. returnValue = self.unescapeFilePostParams(returnValue);
  270. }
  271. return returnValue;
  272. } else {
  273. throw "Invalid function name";
  274. }
  275. };
  276. if (withTimeout) {
  277. setTimeout(callFunction, 0);
  278. } else {
  279. return callFunction();
  280. }
  281. };
  282. /* *****************************
  283. -- Flash control methods --
  284. Your UI should use these
  285. to operate SWFUpload
  286. ***************************** */
  287. // Public: selectFile causes a File Selection Dialog window to appear. This
  288. // dialog only allows 1 file to be selected.
  289. SWFUpload.prototype.selectFile = function () {
  290. this.callFlash("SelectFile");
  291. };
  292. // Public: selectFiles causes a File Selection Dialog window to appear/ This
  293. // dialog allows the user to select any number of files
  294. // Flash Bug Warning: Flash limits the number of selectable files based on the combined length of the file names.
  295. // If the selection name length is too long the dialog will fail in an unpredictable manner. There is no work-around
  296. // for this bug.
  297. SWFUpload.prototype.selectFiles = function () {
  298. this.callFlash("SelectFiles");
  299. };
  300. // Public: startUpload starts uploading the first file in the queue unless
  301. // the optional parameter 'fileID' specifies the ID
  302. SWFUpload.prototype.startUpload = function (fileID) {
  303. // NOTE: Testing this without using a setTimeout. Since StartUpload was reworked to use ReturnUploadStart
  304. // it might not be necessary anymore
  305. this.callFlash("StartUpload", false, [fileID]);
  306. };
  307. /* Cancels a the file upload. You must specify a file_id */
  308. // Public: cancelUpload cancels any queued file. The fileID parameter
  309. // must be specified.
  310. SWFUpload.prototype.cancelUpload = function (fileID) {
  311. this.callFlash("CancelUpload", false, [fileID]);
  312. };
  313. // Public: stopUpload stops the current upload and requeues the file at the beginning of the queue.
  314. // If nothing is currently uploading then nothing happens.
  315. SWFUpload.prototype.stopUpload = function () {
  316. this.callFlash("StopUpload");
  317. };
  318. /* ************************
  319. * Settings methods
  320. * These methods change the SWFUpload settings.
  321. * SWFUpload settings should not be changed directly on the settings object
  322. * since many of the settings need to be passed to Flash in order to take
  323. * effect.
  324. * *********************** */
  325. // Public: getStats gets the file statistics object. It looks like this (where n is a number):
  326. SWFUpload.prototype.getStats = function () {
  327. return this.callFlash("GetStats");
  328. };
  329. // Public: setStats changes the SWFUpload statistics. You shouldn't need to
  330. // change the statistics but you can. Changing the statistics does not
  331. // affect SWFUpload accept for the successful_uploads count which is used
  332. // by the upload_limit setting to determine how many files the user may upload.
  333. SWFUpload.prototype.setStats = function (statsObject) {
  334. this.callFlash("SetStats", false, [statsObject]);
  335. };
  336. // Public: setCredentials that will be used to authenticate to the upload_url.
  337. // Note: This feature does not work. It has been added in anticipation of
  338. // the Flex 3 SDK which has not been released yet.
  339. SWFUpload.prototype.setCredentials = function (name, password) {
  340. this.callFlash("SetCrednetials", false, [name, password]);
  341. };
  342. // Public: getFile retrieves a File object by ID or Index. If the file is
  343. // not found then 'null' is returned.
  344. SWFUpload.prototype.getFile = function (fileID) {
  345. if (typeof(fileID) === "number") {
  346. return this.callFlash("GetFileByIndex", false, [fileID]);
  347. } else {
  348. return this.callFlash("GetFile", false, [fileID]);
  349. }
  350. };
  351. // Public: addFileParam sets a name/value pair that will be posted with the
  352. // file specified by the Files ID. If the name already exists then the
  353. // exiting value will be overwritten.
  354. SWFUpload.prototype.addFileParam = function (fileID, name, value) {
  355. return this.callFlash("AddFileParam", false, [fileID, name, value]);
  356. };
  357. // Public: removeFileParam removes a previously set (by addFileParam) name/value
  358. // pair from the specified file.
  359. SWFUpload.prototype.removeFileParam = function (fileID, name) {
  360. this.callFlash("RemoveFileParam", false, [fileID, name]);
  361. };
  362. // Public: setUploadUrl changes the upload_url setting.
  363. SWFUpload.prototype.setUploadURL = function (url) {
  364. this.settings.upload_url = url.toString();
  365. this.callFlash("SetUploadURL", false, [url]);
  366. };
  367. // Public: setPostParams changes the post_params setting
  368. SWFUpload.prototype.setPostParams = function (paramsObject) {
  369. this.settings.post_params = paramsObject;
  370. this.callFlash("SetPostParams", false, [paramsObject]);
  371. };
  372. // Public: addPostParam adds post name/value pair. Each name can have only one value.
  373. SWFUpload.prototype.addPostParam = function (name, value) {
  374. this.settings.post_params[name] = value;
  375. this.callFlash("SetPostParams", false, [this.settings.post_params]);
  376. };
  377. // Public: removePostParam deletes post name/value pair.
  378. SWFUpload.prototype.removePostParam = function (name) {
  379. delete this.settings.post_params[name];
  380. this.callFlash("SetPostParams", false, [this.settings.post_params]);
  381. };
  382. // Public: setFileTypes changes the file_types setting and the file_types_description setting
  383. SWFUpload.prototype.setFileTypes = function (types, description) {
  384. this.settings.file_types = types;
  385. this.settings.file_types_description = description;
  386. this.callFlash("SetFileTypes", false, [types, description]);
  387. };
  388. // Public: setFileSizeLimit changes the file_size_limit setting
  389. SWFUpload.prototype.setFileSizeLimit = function (fileSizeLimit) {
  390. this.settings.file_size_limit = fileSizeLimit;
  391. this.callFlash("SetFileSizeLimit", false, [fileSizeLimit]);
  392. };
  393. // Public: setFileUploadLimit changes the file_upload_limit setting
  394. SWFUpload.prototype.setFileUploadLimit = function (fileUploadLimit) {
  395. this.settings.file_upload_limit = fileUploadLimit;
  396. this.callFlash("SetFileUploadLimit", false, [fileUploadLimit]);
  397. };
  398. // Public: setFileQueueLimit changes the file_queue_limit setting
  399. SWFUpload.prototype.setFileQueueLimit = function (fileQueueLimit) {
  400. this.settings.file_queue_limit = fileQueueLimit;
  401. this.callFlash("SetFileQueueLimit", false, [fileQueueLimit]);
  402. };
  403. // Public: setFilePostName changes the file_post_name setting
  404. SWFUpload.prototype.setFilePostName = function (filePostName) {
  405. this.settings.file_post_name = filePostName;
  406. this.callFlash("SetFilePostName", false, [filePostName]);
  407. };
  408. // Public: setUseQueryString changes the use_query_string setting
  409. SWFUpload.prototype.setUseQueryString = function (useQueryString) {
  410. this.settings.use_query_string = useQueryString;
  411. this.callFlash("SetUseQueryString", false, [useQueryString]);
  412. };
  413. // Public: setRequeueOnError changes the requeue_on_error setting
  414. SWFUpload.prototype.setRequeueOnError = function (requeueOnError) {
  415. this.settings.requeue_on_error = requeueOnError;
  416. this.callFlash("SetRequeueOnError", false, [requeueOnError]);
  417. };
  418. // Public: setDebugEnabled changes the debug_enabled setting
  419. SWFUpload.prototype.setDebugEnabled = function (debugEnabled) {
  420. this.settings.debug_enabled = debugEnabled;
  421. this.callFlash("SetDebugEnabled", false, [debugEnabled]);
  422. };
  423. /* *******************************
  424. Flash Event Interfaces
  425. These functions are used by Flash to trigger the various
  426. events.
  427. All these functions a Private.
  428. Because the ExternalInterface library is buggy the event calls
  429. are added to a queue and the queue then executed by a setTimeout.
  430. This ensures that events are executed in a determinate order and that
  431. the ExternalInterface bugs are avoided.
  432. ******************************* */
  433. SWFUpload.prototype.queueEvent = function (handlerName, argumentArray) {
  434. // Warning: Don't call this.debug inside here or you'll create an infinite loop
  435. if (argumentArray == undefined) {
  436. argumentArray = [];
  437. } else if (!(argumentArray instanceof Array)) {
  438. argumentArray = [argumentArray];
  439. }
  440. var self = this;
  441. if (typeof(this.settings[handlerName]) === "function") {
  442. // Queue the event
  443. this.eventQueue.push(function () {
  444. this.settings[handlerName].apply(this, argumentArray);
  445. });
  446. // Execute the next queued event
  447. setTimeout(function () {
  448. self.executeNextEvent();
  449. }, 0);
  450. } else if (this.settings[handlerName] !== null) {
  451. throw "Event handler " + handlerName + " is unknown or is not a function";
  452. }
  453. };
  454. SWFUpload.prototype.executeNextEvent = function () {
  455. // Warning: Don't call this.debug inside here or you'll create an infinite loop
  456. var f = this.eventQueue.shift();
  457. f.apply(this);
  458. };
  459. // Private: unescapeFileParams is part of a workaround for a flash bug where objects passed through ExternalInterfance cannot have
  460. // properties that contain characters that are not valid for JavaScript identifiers. To work around this
  461. // the Flash Component escapes the parameter names and we must unescape again before passing them along.
  462. SWFUpload.prototype.unescapeFilePostParams = function (file) {
  463. var reg = /[$]([0-9a-f]{4})/i;
  464. var unescapedPost = {};
  465. var uk;
  466. for (var k in file.post) {
  467. if (file.post.hasOwnProperty(k)) {
  468. uk = k;
  469. var match;
  470. while ((match = reg.exec(uk)) !== null) {
  471. uk = uk.replace(match[0], String.fromCharCode(parseInt("0x"+match[1], 16)));
  472. }
  473. unescapedPost[uk] = file.post[k];
  474. }
  475. }
  476. file.post = unescapedPost;
  477. return file;
  478. };
  479. SWFUpload.prototype.flashReady = function () {
  480. // Check that the movie element is loaded correctly with its ExternalInterface methods defined
  481. var movieElement = this.getMovieElement();
  482. if (typeof(movieElement.StartUpload) !== "function") {
  483. throw "ExternalInterface methods failed to initialize.";
  484. }
  485. this.queueEvent("swfupload_loaded_handler");
  486. };
  487. /* This is a chance to do something before the browse window opens */
  488. SWFUpload.prototype.fileDialogStart = function () {
  489. this.queueEvent("file_dialog_start_handler");
  490. };
  491. /* Called when a file is successfully added to the queue. */
  492. SWFUpload.prototype.fileQueued = function (file) {
  493. file = this.unescapeFilePostParams(file);
  494. this.queueEvent("file_queued_handler", file);
  495. };
  496. /* Handle errors that occur when an attempt to queue a file fails. */
  497. SWFUpload.prototype.fileQueueError = function (file, errorCode, message) {
  498. file = this.unescapeFilePostParams(file);
  499. this.queueEvent("file_queue_error_handler", [file, errorCode, message]);
  500. };
  501. /* Called after the file dialog has closed and the selected files have been queued.
  502. You could call startUpload here if you want the queued files to begin uploading immediately. */
  503. SWFUpload.prototype.fileDialogComplete = function (numFilesSelected, numFilesQueued) {
  504. this.queueEvent("file_dialog_complete_handler", [numFilesSelected, numFilesQueued]);
  505. };
  506. SWFUpload.prototype.uploadStart = function (file) {
  507. file = this.unescapeFilePostParams(file);
  508. this.queueEvent("return_upload_start_handler", file);
  509. };
  510. SWFUpload.prototype.returnUploadStart = function (file) {
  511. var returnValue;
  512. if (typeof(this.settings.upload_start_handler) === "function") {
  513. file = this.unescapeFilePostParams(file);
  514. returnValue = this.settings.upload_start_handler.call(this, file);
  515. } else if (this.settings.upload_start_handler != undefined) {
  516. throw "upload_start_handler must be a function";
  517. }
  518. // Convert undefined to true so if nothing is returned from the upload_start_handler it is
  519. // interpretted as 'true'.
  520. if (returnValue === undefined) {
  521. returnValue = true;
  522. }
  523. returnValue = !!returnValue;
  524. this.callFlash("ReturnUploadStart", false, [returnValue]);
  525. };
  526. SWFUpload.prototype.uploadProgress = function (file, bytesComplete, bytesTotal) {
  527. file = this.unescapeFilePostParams(file);
  528. this.queueEvent("upload_progress_handler", [file, bytesComplete, bytesTotal]);
  529. };
  530. SWFUpload.prototype.uploadError = function (file, errorCode, message) {
  531. file = this.unescapeFilePostParams(file);
  532. this.queueEvent("upload_error_handler", [file, errorCode, message]);
  533. };
  534. SWFUpload.prototype.uploadSuccess = function (file, serverData) {
  535. file = this.unescapeFilePostParams(file);
  536. this.queueEvent("upload_success_handler", [file, serverData]);
  537. };
  538. SWFUpload.prototype.uploadComplete = function (file) {
  539. file = this.unescapeFilePostParams(file);
  540. this.queueEvent("upload_complete_handler", file);
  541. };
  542. /* Called by SWFUpload JavaScript and Flash functions when debug is enabled. By default it writes messages to the
  543. internal debug console. You can override this event and have messages written where you want. */
  544. SWFUpload.prototype.debug = function (message) {
  545. this.queueEvent("debug_handler", message);
  546. };
  547. /* **********************************
  548. Debug Console
  549. The debug console is a self contained, in page location
  550. for debug message to be sent. The Debug Console adds
  551. itself to the body if necessary.
  552. The console is automatically scrolled as messages appear.
  553. If you are using your own debug handler or when you deploy to production and
  554. have debug disabled you can remove these functions to reduce the file size
  555. and complexity.
  556. ********************************** */
  557. // Private: debugMessage is the default debug_handler. If you want to print debug messages
  558. // call the debug() function. When overriding the function your own function should
  559. // check to see if the debug setting is true before outputting debug information.
  560. SWFUpload.prototype.debugMessage = function (message) {
  561. if (this.settings.debug) {
  562. var exceptionMessage, exceptionValues = [];
  563. // Check for an exception object and print it nicely
  564. if (typeof(message) === "object" && typeof(message.name) === "string" && typeof(message.message) === "string") {
  565. for (var key in message) {
  566. if (message.hasOwnProperty(key)) {
  567. exceptionValues.push(key + ": " + message[key]);
  568. }
  569. }
  570. exceptionMessage = exceptionValues.join("\n") || "";
  571. exceptionValues = exceptionMessage.split("\n");
  572. exceptionMessage = "EXCEPTION: " + exceptionValues.join("\nEXCEPTION: ");
  573. SWFUpload.Console.writeLine(exceptionMessage);
  574. } else {
  575. SWFUpload.Console.writeLine(message);
  576. }
  577. }
  578. };
  579. SWFUpload.Console = {};
  580. SWFUpload.Console.writeLine = function (message) {
  581. var console, documentForm;
  582. try {
  583. console = document.getElementById("SWFUpload_Console");
  584. if (!console) {
  585. documentForm = document.createElement("form");
  586. document.getElementsByTagName("body")[0].appendChild(documentForm);
  587. console = document.createElement("textarea");
  588. console.id = "SWFUpload_Console";
  589. console.style.fontFamily = "monospace";
  590. console.setAttribute("wrap", "off");
  591. console.wrap = "off";
  592. console.style.overflow = "auto";
  593. console.style.width = "700px";
  594. console.style.height = "350px";
  595. console.style.margin = "5px";
  596. documentForm.appendChild(console);
  597. }
  598. console.value += message + "\n";
  599. console.scrollTop = console.scrollHeight - console.clientHeight;
  600. } catch (ex) {
  601. alert("Exception: " + ex.name + " Message: " + ex.message);
  602. }
  603. };