KXStudio Website https://kx.studio/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

swfupload.js 33KB

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