handlers.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. [Leo.C, Studio] (C)2004 - 2008
  3. $Hanization: LeoChung $
  4. $E-Mail: who@imll.net $
  5. $HomePage: http://imll.net $
  6. $Date: 2008/11/8 18:02 $
  7. */
  8. /* Demo Note: This demo uses a FileProgress class that handles the UI for displaying the file name and percent complete.
  9. The FileProgress class is not part of SWFUpload.
  10. */
  11. /* **********************
  12. Event Handlers
  13. These are my custom event handlers to make my
  14. web application behave the way I went when SWFUpload
  15. completes different tasks. These aren't part of the SWFUpload
  16. package. They are part of my application. Without these none
  17. of the actions SWFUpload makes will show up in my application.
  18. ********************** */
  19. function fileQueued(file) {
  20. try {
  21. var progress = new FileProgress(file, this.customSettings.progressTarget);
  22. progress.setStatus("正在等待...");
  23. progress.toggleCancel(true, this);
  24. } catch (ex) {
  25. this.debug(ex);
  26. }
  27. }
  28. function fileQueueError(file, errorCode, message) {
  29. try {
  30. if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
  31. alert("您正在上传的文件队列过多.\n" + (message === 0 ? "您已达到上传限制" : "您最多能选择 " + (message > 1 ? "上传 " + message + " 文件." : "一个文件.")));
  32. return;
  33. }
  34. var progress = new FileProgress(file, this.customSettings.progressTarget);
  35. progress.setError();
  36. progress.toggleCancel(false);
  37. switch (errorCode) {
  38. case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
  39. progress.setStatus("文件尺寸过大.");
  40. this.debug("错误代码: 文件尺寸过大, 文件名: " + file.name + ", 文件尺寸: " + file.size + ", 信息: " + message);
  41. break;
  42. case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
  43. progress.setStatus("无法上传零字节文件.");
  44. this.debug("错误代码: 零字节文件, 文件名: " + file.name + ", 文件尺寸: " + file.size + ", 信息: " + message);
  45. break;
  46. case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
  47. progress.setStatus("不支持的文件类型.");
  48. this.debug("错误代码: 不支持的文件类型, 文件名: " + file.name + ", 文件尺寸: " + file.size + ", 信息: " + message);
  49. break;
  50. default:
  51. if (file !== null) {
  52. progress.setStatus("未处理的错误");
  53. }
  54. this.debug("错误代码: " + errorCode + ", 文件名: " + file.name + ", 文件尺寸: " + file.size + ", 信息: " + message);
  55. break;
  56. }
  57. } catch (ex) {
  58. this.debug(ex);
  59. }
  60. }
  61. function fileDialogComplete(numFilesSelected, numFilesQueued) {
  62. try {
  63. if (numFilesSelected > 0) {
  64. document.getElementById(this.customSettings.cancelButtonId).disabled = false;
  65. }
  66. /* I want auto start the upload and I can do that here */
  67. this.startUpload();
  68. } catch (ex) {
  69. this.debug(ex);
  70. }
  71. }
  72. function uploadStart(file) {
  73. try {
  74. /* I don't want to do any file validation or anything, I'll just update the UI and
  75. return true to indicate that the upload should start.
  76. It's important to update the UI here because in Linux no uploadProgress events are called. The best
  77. we can do is say we are uploading.
  78. */
  79. var progress = new FileProgress(file, this.customSettings.progressTarget);
  80. progress.setStatus("正在上传...");
  81. progress.toggleCancel(true, this);
  82. }
  83. catch (ex) {}
  84. return true;
  85. }
  86. function uploadProgress(file, bytesLoaded, bytesTotal) {
  87. try {
  88. var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
  89. var progress = new FileProgress(file, this.customSettings.progressTarget);
  90. //progress.setProgress(percent);
  91. //progress.setStatus("正在上传...");
  92. } catch (ex) {
  93. this.debug(ex);
  94. }
  95. }
  96. function uploadSuccess(file, serverData) {
  97. try {
  98. var progress = new FileProgress(file, this.customSettings.progressTarget);
  99. progress.setComplete();
  100. progress.setStatus("上传成功");
  101. progress.toggleCancel(false);
  102. } catch (ex) {
  103. this.debug(ex);
  104. }
  105. }
  106. function uploadError(file, errorCode, message) {
  107. try {
  108. var progress = new FileProgress(file, this.customSettings.progressTarget);
  109. progress.setError();
  110. progress.toggleCancel(false);
  111. switch (errorCode) {
  112. case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
  113. progress.setStatus("上传错误: " + message);
  114. this.debug("错误代码: HTTP错误, 文件名: " + file.name + ", 信息: " + message);
  115. break;
  116. case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
  117. progress.setStatus("上传失败");
  118. this.debug("错误代码: 上传失败, 文件名: " + file.name + ", 文件尺寸: " + file.size + ", 信息: " + message);
  119. break;
  120. case SWFUpload.UPLOAD_ERROR.IO_ERROR:
  121. progress.setStatus("服务器 (IO) 错误");
  122. this.debug("错误代码: IO 错误, 文件名: " + file.name + ", 信息: " + message);
  123. break;
  124. case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
  125. progress.setStatus("安全错误");
  126. this.debug("错误代码: 安全错误, 文件名: " + file.name + ", 信息: " + message);
  127. break;
  128. case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
  129. progress.setStatus("超出上传限制.");
  130. this.debug("错误代码: 超出上传限制, 文件名: " + file.name + ", 文件尺寸: " + file.size + ", 信息: " + message);
  131. break;
  132. case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
  133. progress.setStatus("无法验证. 跳过上传.");
  134. this.debug("错误代码: 文件验证失败, 文件名: " + file.name + ", 文件尺寸: " + file.size + ", 信息: " + message);
  135. break;
  136. case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
  137. // If there aren't any files left (they were all cancelled) disable the cancel button
  138. if (this.getStats().files_queued === 0) {
  139. document.getElementById(this.customSettings.cancelButtonId).disabled = true;
  140. }
  141. progress.setStatus("取消");
  142. progress.setCancelled();
  143. break;
  144. case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
  145. progress.setStatus("停止");
  146. break;
  147. default:
  148. progress.setStatus("未处理的错误: " + errorCode);
  149. this.debug("错误代码: " + errorCode + ", 文件名: " + file.name + ", 文件尺寸: " + file.size + ", 信息: " + message);
  150. break;
  151. }
  152. } catch (ex) {
  153. this.debug(ex);
  154. }
  155. }
  156. function uploadComplete(file) {
  157. if (this.getStats().files_queued === 0) {
  158. var progress = new FileProgress(file, this.customSettings.progressTarget);
  159. progress.setComplete();
  160. progress.setStatus("上传成功");
  161. progress.toggleCancel(false);
  162. document.getElementById(this.customSettings.cancelButtonId).disabled = true;
  163. }
  164. }
  165. // This event comes from the Queue Plugin
  166. function queueComplete(numFilesUploaded) {
  167. var status = document.getElementById("divStatus");
  168. status.innerHTML = numFilesUploaded + " 个文件" + (numFilesUploaded === 1 ? "" : "s") + "已上传.";
  169. }