PageRenderTime 28ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/public/javascripts/swfupload.js

http://pmsaas.googlecode.com/
JavaScript | 803 lines | 592 code | 106 blank | 105 comment | 98 complexity | ff7b231333044dbcc16ab466e6e02f03 MD5 | raw file
  1. /**
  2. * SWFUpload 0.8.3 Revision 6.2 by Jacob Roberts, July 2007, linebyline.blogspot.com
  3. * -------- -------- -------- -------- -------- -------- -------- --------
  4. * SWFUpload is (c) 2006 Lars Huring 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. * + Added ServerData event callback
  10. * = Changed upload_params to query_params
  11. * + Added post_params
  12. * + Added PreuploadValidation callback
  13. * + Added automatic cookie transmission to workaround the Flash browser bug
  14. * ? Added custom file post name setting
  15. */
  16. /* *********** */
  17. /* Constructor */
  18. /* *********** */
  19. var SWFUpload = function (init_settings) {
  20. // Remove background flicker in IE (read this: http://misterpixel.blogspot.com/2006/09/forensic-analysis-of-ie6.html)
  21. // This doesn't have anything to do with SWFUpload but can help your UI behave better
  22. try {
  23. document.execCommand('BackgroundImageCache', false, true);
  24. } catch (ex) {
  25. this.debugMessage(ex);
  26. }
  27. try {
  28. this.settings = {};
  29. this.movieName = "SWFUpload_" + SWFUpload.movieCount++;
  30. this.movieElement = null;
  31. // Setup global control tracking
  32. SWFUpload.instances[this.movieName] = this;
  33. // Load the settings. Load the Flash movie.
  34. this.initSettings(init_settings);
  35. this.loadFlash();
  36. if (this.debug_enabled) {
  37. this.displayDebugInfo();
  38. }
  39. } catch (ex) {
  40. this.debugMessage(ex);
  41. }
  42. };
  43. /* *************** */
  44. /* Static thingies */
  45. /* *************** */
  46. SWFUpload.instances = {};
  47. SWFUpload.movieCount = 0;
  48. SWFUpload.ERROR_CODE_HTTP_ERROR = -10;
  49. SWFUpload.ERROR_CODE_MISSING_UPLOAD_TARGET = -20;
  50. SWFUpload.ERROR_CODE_IO_ERROR = -30;
  51. SWFUpload.ERROR_CODE_SECURITY_ERROR = -40;
  52. SWFUpload.ERROR_CODE_FILE_EXCEEDS_SIZE_LIMIT = -50;
  53. SWFUpload.ERROR_CODE_ZERO_BYTE_FILE = -60;
  54. SWFUpload.ERROR_CODE_UPLOAD_LIMIT_EXCEEDED = -70;
  55. SWFUpload.ERROR_CODE_UPLOAD_FAILED = -80;
  56. SWFUpload.ERROR_CODE_QUEUE_LIMIT_EXCEEDED = -90;
  57. SWFUpload.ERROR_CODE_SPECIFIED_FILE_NOT_FOUND = -100;
  58. SWFUpload.ERROR_CODE_INVALID_FILETYPE = -110;
  59. /* ***************** */
  60. /* Instance Thingies */
  61. /* ***************** */
  62. // init is a private method that ensures that all the object settings are set, getting a default value if one was not assigned.
  63. SWFUpload.prototype.initSettings = function (init_settings) {
  64. this.addSetting("control_id", this.movieName, "");
  65. // UI setting
  66. this.addSetting("ui_function", init_settings.ui_function, null);
  67. this.addSetting("ui_container_id", init_settings.ui_container_id, "");
  68. this.addSetting("degraded_container_id", init_settings.degraded_container_id, "");
  69. // Upload backend settings
  70. this.addSetting("upload_target_url", init_settings.upload_target_url, "");
  71. this.addSetting("file_post_name", init_settings.file_post_name, "Filedata");
  72. this.addSetting("post_params", init_settings.post_params, {});
  73. // Flags
  74. this.addSetting("begin_upload_on_queue", init_settings.begin_upload_on_queue, true);
  75. this.addSetting("use_server_data_event", init_settings.use_server_data_event, false);
  76. this.addSetting("validate_files", init_settings.validate_files, false);
  77. // File Settings
  78. this.addSetting("file_types", init_settings.file_types, "*.gif;*.jpg;*.png");
  79. this.addSetting("file_types_description", init_settings.file_types_description, "Common Web Image Formats (gif, jpg, png)");
  80. this.addSetting("file_size_limit", init_settings.file_size_limit, "1024");
  81. this.addSetting("file_upload_limit", init_settings.file_upload_limit, "0");
  82. this.addSetting("file_queue_limit", init_settings.file_queue_limit, "0");
  83. // Flash Settings
  84. this.addSetting("flash_url", init_settings.flash_url, "swfupload.swf");
  85. this.addSetting("flash_width", init_settings.flash_width, "1px");
  86. this.addSetting("flash_height", init_settings.flash_height, "1px");
  87. this.addSetting("flash_color", init_settings.flash_color, "#FFFFFF");
  88. // Debug Settings
  89. this.addSetting("debug_enabled", init_settings.debug, false);
  90. this.debug_enabled = this.getSetting("debug_enabled");
  91. // Event Handlers
  92. this.flashReady = this.retrieveSetting(init_settings.flash_ready_handler, this.flashReady);
  93. this.dialogCancelled = this.retrieveSetting(init_settings.dialog_cancelled_handler, this.dialogCancelled);
  94. this.fileQueued = this.retrieveSetting(init_settings.file_queued_handler, this.fileQueued);
  95. this.fileValidation = this.retrieveSetting(init_settings.file_validation_handler, this.fileValidation);
  96. this.fileProgress = this.retrieveSetting(init_settings.file_progress_handler, this.fileProgress);
  97. this.fileCancelled = this.retrieveSetting(init_settings.file_cancelled_handler, this.fileCancelled);
  98. this.fileComplete = this.retrieveSetting(init_settings.file_complete_handler, this.fileComplete);
  99. this.queueStopped = this.retrieveSetting(init_settings.queue_stopped_handler, this.queueStopped);
  100. this.queueComplete = this.retrieveSetting(init_settings.queue_complete_handler, this.queueComplete);
  101. this.error = this.retrieveSetting(init_settings.error_handler, this.error);
  102. this.debug = this.retrieveSetting(init_settings.debug_handler, this.debug);
  103. };
  104. // loadFlash is a private method that generates the HTML tag for the Flash
  105. // It then adds the flash to the "target" or to the body and stores a
  106. // reference to the flash element in "movieElement".
  107. SWFUpload.prototype.loadFlash = function () {
  108. var html, target_element, container;
  109. // Make sure an element with the ID we are going to use doesn't already exist
  110. if (document.getElementById(this.movieName) !== null) {
  111. return false;
  112. }
  113. // Get the body tag where we will be adding the flash movie
  114. target_element = document.getElementsByTagName("body")[0];
  115. if (typeof(target_element) === "undefined" || target_element === null) {
  116. this.debugMessage('Could not find an element to add the Flash too. Failed to find element for "flash_container_id" or the BODY element.');
  117. return false;
  118. }
  119. // Append the container and load the flash
  120. container = document.createElement("div");
  121. container.style.width = this.getSetting("flash_width");
  122. container.style.height = this.getSetting("flash_height");
  123. target_element.appendChild(container);
  124. container.innerHTML = this.getFlashHTML(); // Using innerHTML is non-standard but the only sensible way to dynamically add Flash in IE (and maybe other browsers)
  125. };
  126. // Generates the embed/object tags needed to embed the flash in to the document
  127. SWFUpload.prototype.getFlashHTML = function () {
  128. var html = "";
  129. // Create Mozilla Embed HTML
  130. if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {
  131. // Build the basic embed html
  132. html = '<embed type="application/x-shockwave-flash" src="' + this.getSetting("flash_url") + '" width="' + this.getSetting("flash_width") + '" height="' + this.getSetting("flash_height") + '"';
  133. html += ' id="' + this.movieName + '" name="' + this.movieName + '" ';
  134. html += 'bgcolor="' + this.getSetting("flash_color") + '" quality="high" menu="false" flashvars="';
  135. html += this.getFlashVars();
  136. html += '" />';
  137. // Create IE Object HTML
  138. } else {
  139. // Build the basic Object tag
  140. html = '<object id="' + this.movieName + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + this.getSetting("flash_width") + '" height="' + this.getSetting("flash_height") + '">';
  141. html += '<param name="movie" value="' + this.getSetting("flash_url") + '">';
  142. html += '<param name="bgcolor" value="' + this.getSetting("flash_color") + '" />';
  143. html += '<param name="quality" value="high" />';
  144. html += '<param name="menu" value="false" />';
  145. html += '<param name="flashvars" value="' + this.getFlashVars() + '" />';
  146. html += '</object>';
  147. }
  148. return html;
  149. };
  150. // This private method builds the parameter string that will be passed
  151. // to flash.
  152. SWFUpload.prototype.getFlashVars = function () {
  153. // Add the cookies to the backend string
  154. var upload_target_url = this.getSetting("upload_target_url");
  155. var param_string = this.buildParamString();
  156. // Build the parameter string
  157. var html = "";
  158. html += "controlID=" + encodeURIComponent(this.getSetting("control_id"));
  159. html += "&uploadTargetURL=" + encodeURIComponent(upload_target_url);
  160. html += "&params=" + encodeURIComponent(param_string);
  161. html += "&filePostName=" + encodeURIComponent(this.getSetting("file_post_name"));
  162. html += "&beginUploadOnQueue=" + encodeURIComponent(this.getSetting("begin_upload_on_queue"));
  163. html += "&useServerDataEvent=" + encodeURIComponent(this.getSetting("use_server_data_event"));
  164. html += "&fileValidation=" + encodeURIComponent(this.getSetting("validate_files"));
  165. html += "&fileTypes=" + encodeURIComponent(this.getSetting("file_types"));
  166. html += "&fileTypesDescription=" + encodeURIComponent(this.getSetting("file_types_description"));
  167. html += "&fileSizeLimit=" + encodeURIComponent(this.getSetting("file_size_limit"));
  168. html += "&fileUploadLimit=" + encodeURIComponent(this.getSetting("file_upload_limit"));
  169. html += "&fileQueueLimit=" + encodeURIComponent(this.getSetting("file_queue_limit"));
  170. html += "&debugEnabled=" + encodeURIComponent(this.getSetting("debug_enabled"));
  171. return html;
  172. };
  173. SWFUpload.prototype.getMovieElement = function () {
  174. if (typeof(this.movieElement) === "undefined" || this.movieElement === null) {
  175. this.movieElement = document.getElementById(this.movieName);
  176. // Fix IEs "Flash can't callback when in a form" issue (http://www.extremefx.com.ar/blog/fixing-flash-external-interface-inside-form-on-internet-explorer)
  177. // Removed because Revision 6 always adds the flash to the body (inside a containing div)
  178. //if (typeof(window[this.movieName]) === "undefined" || window[this.moveName] !== this.movieElement) {
  179. // window[this.movieName] = this.movieElement;
  180. //}
  181. }
  182. return this.movieElement;
  183. }
  184. SWFUpload.prototype.buildParamString = function () {
  185. var post_params = this.getSetting("post_params");
  186. var param_string_pairs = [];
  187. var i, value, name;
  188. // Retrieve the user defined parameters
  189. if (typeof(post_params) === "object") {
  190. for (name in post_params) {
  191. if (post_params.hasOwnProperty(name)) {
  192. if (typeof(post_params[name]) === "string" /*&& upload_params[name] != ""*/) {
  193. param_string_pairs.push(encodeURIComponent(name) + "=" + encodeURIComponent(post_params[name]));
  194. }
  195. }
  196. }
  197. }
  198. return param_string_pairs.join("&");
  199. };
  200. // This private method "loads" the UI. If a target was specified then it is assumed that "display: none" was set and
  201. // it does a "display: block" so the UI is shown. Then if a degraded_target is specified it hides it by setting "display: none"
  202. // If you want SWFUpload to do something else then provide a "ui_function" setting and that will be called instead.
  203. SWFUpload.prototype.showUI = function () {
  204. var ui_container_id, ui_target, degraded_container_id, degraded_target;
  205. try {
  206. ui_container_id = this.getSetting("ui_container_id");
  207. if (ui_container_id !== "") {
  208. ui_target = document.getElementById(ui_container_id);
  209. if (ui_target !== null) {
  210. ui_target.style.display = "block";
  211. // Now that the UI has been taken care of hide the degraded UI
  212. degraded_container_id = this.getSetting("degraded_container_id");
  213. if (degraded_container_id !== "") {
  214. degraded_target = document.getElementById(degraded_container_id);
  215. if (degraded_target !== null) {
  216. degraded_target.style.display = "none";
  217. }
  218. }
  219. }
  220. }
  221. } catch (ex) {
  222. this.debugMessage(ex);
  223. }
  224. };
  225. // Saves a setting. If the value given is undefined or null then the default_value is used.
  226. SWFUpload.prototype.addSetting = function (name, value, default_value) {
  227. if (typeof(value) === "undefined" || value === null) {
  228. this.settings[name] = default_value;
  229. } else {
  230. this.settings[name] = value;
  231. }
  232. return this.settings[name];
  233. };
  234. // Gets a setting. Returns null if it wasn't found.
  235. SWFUpload.prototype.getSetting = function (name) {
  236. if (typeof(this.settings[name]) === "undefined") {
  237. return "";
  238. } else {
  239. return this.settings[name];
  240. }
  241. };
  242. // Gets a setting, if the setting is undefined then return the default value
  243. // This does not affect or use the interal setting object.
  244. SWFUpload.prototype.retrieveSetting = function (value, default_value) {
  245. if (typeof(value) === "undefined" || value === null) {
  246. return default_value;
  247. } else {
  248. return value;
  249. }
  250. };
  251. // This method is used when debugging is enabled.
  252. // It loops through all the settings and displays
  253. // them in the debug Console.
  254. SWFUpload.prototype.displayDebugInfo = function () {
  255. var key, debug_message = "";
  256. debug_message += "----- DEBUG OUTPUT ----\nID: " + this.getMovieElement().id + "\n";
  257. debug_message += this.outputObject(this.settings);
  258. debug_message += "----- DEBUG OUTPUT END ----\n";
  259. debug_message += "\n";
  260. this.debugMessage(debug_message);
  261. };
  262. SWFUpload.prototype.outputObject = function (object, prefix) {
  263. var output = "";
  264. if (typeof(prefix) !== "string") {
  265. prefix = "";
  266. }
  267. if (typeof(object) !== "object") {
  268. return "";
  269. }
  270. for (key in object) {
  271. if (object.hasOwnProperty(key)) {
  272. if (typeof(object[key]) === "object") {
  273. output += (prefix + key + ": { \n" + this.outputObject(object[key], "\t" + prefix) + prefix + "}" + "\n");
  274. } else {
  275. output += (prefix + key + ": " + object[key] + "\n");
  276. }
  277. }
  278. }
  279. return output;
  280. };
  281. /* *****************************
  282. -- Flash control methods --
  283. Your UI should use these
  284. to operate SWFUpload
  285. ***************************** */
  286. SWFUpload.prototype.browse = function () {
  287. var movie_element = this.getMovieElement();
  288. if (movie_element !== null && typeof(movie_element.Browse) === "function") {
  289. try {
  290. movie_element.Browse();
  291. }
  292. catch (ex) {
  293. this.debugMessage("Could not call browse: " + ex);
  294. }
  295. } else {
  296. this.debugMessage("Could not find Flash element");
  297. }
  298. };
  299. // Begins the uploads (if begin_upload_on_queue is disabled)
  300. // The file_id is optional. If specified only that file will be uploaded. If not specified SWFUpload will
  301. // begin to process the queue.
  302. SWFUpload.prototype.startUpload = function (file_id) {
  303. var movie_element = this.getMovieElement();
  304. if (movie_element !== null && typeof(movie_element.StartUpload) === "function") {
  305. try {
  306. movie_element.StartUpload(file_id);
  307. }
  308. catch (ex) {
  309. this.debugMessage("Could not call StartUpload: " + ex);
  310. }
  311. } else {
  312. this.debugMessage("Could not find Flash element");
  313. }
  314. };
  315. // Cancels the current uploading item. If no item is uploading then nothing happens.
  316. SWFUpload.prototype.cancelUpload = function (file_id) {
  317. var movie_element = this.getMovieElement();
  318. if (movie_element !== null && typeof(movie_element.CancelUpload) === "function") {
  319. try {
  320. movie_element.CancelUpload(file_id);
  321. }
  322. catch (ex) {
  323. this.debugMessage("Could not call CancelUpload");
  324. }
  325. } else {
  326. this.debugMessage("Could not find Flash element");
  327. }
  328. };
  329. // Cancels all the files in the queue. Including any current uploads.
  330. SWFUpload.prototype.cancelQueue = function () {
  331. var movie_element = this.getMovieElement();
  332. if (movie_element !== null && typeof(movie_element.CancelQueue) === "function") {
  333. try {
  334. movie_element.CancelQueue();
  335. }
  336. catch (ex) {
  337. this.debugMessage("Could not call CancelQueue");
  338. }
  339. } else {
  340. this.debugMessage("Could not find Flash element");
  341. }
  342. };
  343. // Stops the current upload. The file is re-queued. If nothing is currently uploading then nothing happens.
  344. SWFUpload.prototype.stopUpload = function () {
  345. var movie_element = this.getMovieElement();
  346. if (movie_element !== null && typeof(movie_element.StopUpload) === "function") {
  347. try {
  348. movie_element.StopUpload();
  349. }
  350. catch (ex) {
  351. this.debugMessage("Could not call StopUpload");
  352. }
  353. } else {
  354. this.debugMessage("Could not find Flash element");
  355. }
  356. };
  357. SWFUpload.prototype.addFileParam = function (file_id, name, value) {
  358. var movie_element = this.getMovieElement();
  359. if (movie_element !== null && typeof(movie_element.AddFileParam) === "function") {
  360. try {
  361. return movie_element.AddFileParam(file_id, name, value);
  362. }
  363. catch (ex) {
  364. this.debugMessage("Could not call AddFileParam");
  365. }
  366. } else {
  367. this.debugMessage("Could not find Flash element");
  368. }
  369. };
  370. SWFUpload.prototype.removeFileParam = function (file_id, name) {
  371. var movie_element = this.getMovieElement();
  372. if (movie_element !== null && typeof(movie_element.RemoveFileParam) === "function") {
  373. try {
  374. return movie_element.RemoveFileParam(file_id, name);
  375. }
  376. catch (ex) {
  377. this.debugMessage("Could not call AddFileParam");
  378. }
  379. } else {
  380. this.debugMessage("Could not find Flash element");
  381. }
  382. };
  383. SWFUpload.prototype.setUploadTargetURL = function (url) {
  384. var movie_element = this.getMovieElement();
  385. if (movie_element !== null && typeof(movie_element.SetUploadTargetURL) === "function") {
  386. try {
  387. this.addSetting("upload_target_url", url);
  388. movie_element.SetUploadTargetURL(this.getSetting("upload_target_url"));
  389. }
  390. catch (ex) {
  391. this.debugMessage("Could not call SetUploadTargetURL");
  392. }
  393. } else {
  394. this.debugMessage("Could not find Flash element in SetUploadTargetURL");
  395. }
  396. };
  397. SWFUpload.prototype.setPostParams = function (param_object) {
  398. var movie_element = this.getMovieElement();
  399. if (movie_element !== null && typeof(movie_element.SetPostParams) === "function") {
  400. try {
  401. this.addSetting("post_params", param_object);
  402. movie_element.SetPostParams(this.getSetting("post_params"));
  403. }
  404. catch (ex) {
  405. this.debugMessage("Could not call SetPostParams");
  406. }
  407. } else {
  408. this.debugMessage("Could not find Flash element in SetPostParams");
  409. }
  410. };
  411. SWFUpload.prototype.setFileTypes = function (types, description) {
  412. var movie_element = this.getMovieElement();
  413. if (movie_element !== null && typeof(movie_element.SetFileTypes) === "function") {
  414. try {
  415. this.addSetting("file_types", types);
  416. this.addSetting("file_types_description", description);
  417. movie_element.SetFileTypes(this.getSetting("file_types"), this.getSetting("file_types_description"));
  418. }
  419. catch (ex) {
  420. this.debugMessage("Could not call SetFileTypes");
  421. }
  422. } else {
  423. this.debugMessage("Could not find Flash element in SetFileTypes");
  424. }
  425. };
  426. SWFUpload.prototype.setFileSizeLimit = function (file_size_limit) {
  427. var movie_element = this.getMovieElement();
  428. if (movie_element !== null && typeof(movie_element.SetFileSizeLimit) === "function") {
  429. try {
  430. this.addSetting("file_size_limit", file_size_limit);
  431. movie_element.SetFileSizeLimit(this.getSetting("file_size_limit"));
  432. }
  433. catch (ex) {
  434. this.debugMessage("Could not call SetFileSizeLimit");
  435. }
  436. } else {
  437. this.debugMessage("Could not find Flash element in SetFileSizeLimit");
  438. }
  439. };
  440. SWFUpload.prototype.setFileUploadLimit = function (file_upload_limit) {
  441. var movie_element = this.getMovieElement();
  442. if (movie_element !== null && typeof(movie_element.SetFileUploadLimit) === "function") {
  443. try {
  444. this.addSetting("file_upload_limit", file_upload_limit);
  445. movie_element.SetFileUploadLimit(this.getSetting("file_upload_limit"));
  446. }
  447. catch (ex) {
  448. this.debugMessage("Could not call SetFileUploadLimit");
  449. }
  450. } else {
  451. this.debugMessage("Could not find Flash element in SetFileUploadLimit");
  452. }
  453. };
  454. SWFUpload.prototype.setFileQueueLimit = function (file_queue_limit) {
  455. var movie_element = this.getMovieElement();
  456. if (movie_element !== null && typeof(movie_element.SetFileQueueLimit) === "function") {
  457. try {
  458. this.addSetting("file_queue_limit", file_queue_limit);
  459. movie_element.SetFileQueueLimit(this.getSetting("file_queue_limit"));
  460. }
  461. catch (ex) {
  462. this.debugMessage("Could not call SetFileQueueLimit");
  463. }
  464. } else {
  465. this.debugMessage("Could not find Flash element in SetFileQueueLimit");
  466. }
  467. };
  468. SWFUpload.prototype.setBeginUploadOnQueue = function (begin_upload_on_queue) {
  469. var movie_element = this.getMovieElement();
  470. if (movie_element !== null && typeof(movie_element.SetBeginUploadOnQueue) === "function") {
  471. try {
  472. this.addSetting("begin_upload_on_queue", begin_upload_on_queue);
  473. movie_element.SetBeginUploadOnQueue(this.getSetting("begin_upload_on_queue"));
  474. }
  475. catch (ex) {
  476. this.debugMessage("Could not call SetBeginUploadOnQueue");
  477. }
  478. } else {
  479. this.debugMessage("Could not find Flash element in SetBeginUploadOnQueue");
  480. }
  481. };
  482. SWFUpload.prototype.setUseServerDataEvent = function (use_server_data_event) {
  483. var movie_element = this.getMovieElement();
  484. if (movie_element !== null && typeof(movie_element.SetUseServerDataEvent) === "function") {
  485. try {
  486. this.addSetting("use_server_data_event", use_server_data_event);
  487. movie_element.SetUseServerDataEvent(this.getSetting("use_server_data_event"));
  488. }
  489. catch (ex) {
  490. this.debugMessage("Could not call SetUseServerDataEvent");
  491. }
  492. } else {
  493. this.debugMessage("Could not find Flash element in SetUseServerDataEvent");
  494. }
  495. };
  496. SWFUpload.prototype.setValidateFiles = function (validate_files) {
  497. var movie_element = this.getMovieElement();
  498. if (movie_element !== null && typeof(movie_element.SetValidateFiles) === "function") {
  499. try {
  500. this.addSetting("validate_files", validate_files);
  501. movie_element.SetValidateFiles(this.getSetting("validate_files"));
  502. }
  503. catch (ex) {
  504. this.debugMessage("Could not call SetValidateFiles");
  505. }
  506. } else {
  507. this.debugMessage("Could not find Flash element in SetValidateFiles");
  508. }
  509. };
  510. SWFUpload.prototype.setFilePostName = function (file_post_name) {
  511. var movie_element = this.getMovieElement();
  512. if (movie_element !== null && typeof(movie_element.SetFilePostName) === "function") {
  513. try {
  514. this.addSetting("file_post_name", file_post_name);
  515. movie_element.SetFilePostName(this.getSetting("file_post_name"));
  516. }
  517. catch (ex) {
  518. this.debugMessage("Could not call SetFilePostName");
  519. }
  520. } else {
  521. this.debugMessage("Could not find Flash element in SetFilePostName");
  522. }
  523. };
  524. SWFUpload.prototype.setDebugEnabled = function (debug_enabled) {
  525. var movie_element = this.getMovieElement();
  526. if (movie_element !== null && typeof(movie_element.SetDebugEnabled) === "function") {
  527. try {
  528. this.addSetting("debug_enabled", debug_enabled);
  529. this.debug_enabled = this.getSetting("debug_enabled");
  530. movie_element.SetDebugEnabled(this.getSetting("debug_enabled"));
  531. }
  532. catch (ex) {
  533. this.debugMessage("Could not call SetDebugEnabled");
  534. }
  535. } else {
  536. this.debugMessage("Could not find Flash element in SetDebugEnabled");
  537. }
  538. };
  539. /* *******************************
  540. Default Event Handlers
  541. ******************************* */
  542. // This is the callback method that the Flash movie will call when it has been loaded and is ready to go.
  543. // Calling this or showUI "manually" bypass the Flash Detection built in to SWFUpload.
  544. // FlashReady should not generally be overwritten. Use a ui_function setting if you want to control the UI loading after the flash has loaded.
  545. SWFUpload.prototype.flashReady = function () {
  546. var ui_function;
  547. try {
  548. this.debugMessage("Flash called back and is ready.");
  549. ui_function = this.getSetting("ui_function");
  550. if (typeof(ui_function) === "function") {
  551. ui_function.apply(this);
  552. } else {
  553. this.showUI();
  554. }
  555. } catch (ex) {
  556. this.debugMessage(ex);
  557. }
  558. };
  559. // Called when the user cancels the File browser window.
  560. SWFUpload.prototype.dialogCancelled = function () {
  561. this.debugMessage("browse Dialog Cancelled.");
  562. };
  563. // Called once for each file the user selects
  564. SWFUpload.prototype.fileQueued = function (file) {
  565. this.debugMessage("File Queued: " + file.id);
  566. };
  567. // Called before a file is queued if the validateFiles setting is true. This function
  568. // must return true or false to indicate to flash whether the file should be
  569. // uploaded.
  570. SWFUpload.prototype.fileValidation = function (file) {
  571. this.debugMessage("File Validation: " + file.id);
  572. return true;
  573. };
  574. // Called during upload as the file progresses
  575. SWFUpload.prototype.fileProgress = function (file, bytes_complete) {
  576. this.debugMessage("File Progress: " + file.id + ", Bytes: " + bytes_complete);
  577. };
  578. // Called after a file is cancelled
  579. SWFUpload.prototype.fileCancelled = function (file) {
  580. this.debugMessage("File Cancelled: " + file.id);
  581. };
  582. // Called when a file upload has completed
  583. SWFUpload.prototype.fileComplete = function (file, server_data) {
  584. this.debugMessage("File Complete: " + file.id);
  585. if (typeof(server_data) !== "undefined") {
  586. this.debugMessage("Upload Response Data: " + server_data);
  587. }
  588. };
  589. // Called when at least 1 file has been uploaded and there are no files remaining in the queue.
  590. SWFUpload.prototype.queueComplete = function (file_upload_count) {
  591. this.debugMessage("Queue Complete. Files Uploaded:" + file_upload_count);
  592. };
  593. // Called when the upload is stopped.
  594. SWFUpload.prototype.queueStopped = function (file) {
  595. this.debugMessage("Queue Stopped. File Stopped:" + file.id);
  596. };
  597. // Called by SWFUpload JavaScript and Flash flash functions when debug is enabled
  598. SWFUpload.prototype.debug = function (message) {
  599. this.debugMessage(message);
  600. };
  601. // Called when an error occurs. Use errcode to determine which error occurred.
  602. SWFUpload.prototype.error = function (errcode, file, msg) {
  603. try {
  604. switch (errcode) {
  605. case SWFUpload.ERROR_CODE_HTTP_ERROR:
  606. this.debugMessage("Error Code: HTTP Error, File name: " + file.name + ", Message: " + msg);
  607. break;
  608. case SWFUpload.ERROR_CODE_MISSING_UPLOAD_TARGET:
  609. this.debugMessage("Error Code: No backend file, File name: " + file.name + ", Message: " + msg);
  610. break;
  611. case SWFUpload.ERROR_CODE_IO_ERROR:
  612. this.debugMessage("Error Code: IO Error, File name: " + file.name + ", Message: " + msg);
  613. break;
  614. case SWFUpload.ERROR_CODE_SECURITY_ERROR:
  615. this.debugMessage("Error Code: Security Error, File name: " + file.name + ", Message: " + msg);
  616. break;
  617. case SWFUpload.ERROR_CODE_FILE_EXCEEDS_SIZE_LIMIT:
  618. this.debugMessage("Error Code: File too big, File name: " + file.name + ", File size: " + file.size + ", Message: " + msg);
  619. break;
  620. case SWFUpload.ERROR_CODE_ZERO_BYTE_FILE:
  621. this.debugMessage("Error Code: Zero Byte File, File name: " + file.name + ", File size: " + file.size + ", Message: " + msg);
  622. break;
  623. case SWFUpload.ERROR_CODE_UPLOAD_LIMIT_EXCEEDED:
  624. this.debugMessage("Error Code: Upload limit reached, File name: " + file.name + ", File size: " + file.size + ", Message: " + msg);
  625. break;
  626. case SWFUpload.ERROR_CODE_QUEUE_LIMIT_EXCEEDED:
  627. this.debugMessage("Error Code: Upload limit reached, File name: " + file.name + ", File size: " + file.size + ", Message: " + msg);
  628. break;
  629. case SWFUpload.ERROR_CODE_UPLOAD_FAILED:
  630. this.debugMessage("Error Code: Upload Initialization exception, File name: " + file.name + ", File size: " + file.size + ", Message: " + msg);
  631. break;
  632. case SWFUpload.ERROR_CODE_SPECIFIED_FILE_NOT_FOUND:
  633. this.debugMessage("Error Code: File ID specified for upload was not found, Message: " + msg);
  634. break;
  635. case SWFUpload.ERROR_CODE_INVALID_FILETYPE:
  636. this.debugMessage("Error Code: File extension is not allowed, Message: " + msg);
  637. break;
  638. default:
  639. this.debugMessage("Error Code: Unhandled error occured. Errorcode: " + errcode);
  640. }
  641. } catch (ex) {
  642. this.debugMessage(ex);
  643. }
  644. };
  645. /* **********************************
  646. Debug Console
  647. The debug console is a self contained, in page location
  648. for debug message to be sent. The Debug Console adds
  649. itself to the body if necessary.
  650. The console is automatically scrolled as messages appear.
  651. ********************************** */
  652. SWFUpload.prototype.debugMessage = function (message) {
  653. var exception_message, exception_values;
  654. if (this.debug_enabled) {
  655. if (typeof(message) === "object" && typeof(message.name) === "string" && typeof(message.message) === "string") {
  656. exception_message = "";
  657. exception_values = [];
  658. for (var key in message) {
  659. exception_values.push(key + ": " + message[key]);
  660. }
  661. exception_message = exception_values.join("\n");
  662. exception_values = exception_message.split("\n");
  663. exception_message = "EXCEPTION: " + exception_values.join("\nEXCEPTION: ");
  664. SWFUpload.Console.writeLine(exception_message);
  665. } else {
  666. SWFUpload.Console.writeLine(message);
  667. }
  668. }
  669. };
  670. SWFUpload.Console = {};
  671. SWFUpload.Console.writeLine = function (message) {
  672. var console, documentForm;
  673. try {
  674. console = document.getElementById("SWFUpload_Console");
  675. if (!console) {
  676. documentForm = document.createElement("form");
  677. document.getElementsByTagName("body")[0].appendChild(documentForm);
  678. console = document.createElement("textarea");
  679. console.id = "SWFUpload_Console";
  680. console.style.fontFamily = "monospace";
  681. console.setAttribute("wrap", "off");
  682. console.wrap = "off";
  683. console.style.overflow = "auto";
  684. console.style.width = "700px";
  685. console.style.height = "350px";
  686. console.style.margin = "5px";
  687. documentForm.appendChild(console);
  688. }
  689. console.value += message + "\n";
  690. console.scrollTop = console.scrollHeight - console.clientHeight;
  691. } catch (ex) {
  692. alert("Exception: " + ex.name + " Message: " + ex.message);
  693. }
  694. };