handlers.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. var ids =new Array();
  2. function add_uploadok(data)
  3. {
  4. var id = data.aid;
  5. var src = data.filepath;
  6. var ext = data.isimage;
  7. var name = data.filename;
  8. var filesize = data.filesize;
  9. if(ext == 1) {
  10. var img = '<a href="javascript:void(0);" onclick="javascript:add_file(this,'+id+')" id="on_'+id+'" class="on"><div class="icon"></div><img src="'+src+'" width="80" alt="'+name+'" imgid="'+id+'" path="'+src+'"/></a>';
  11. } else {
  12. var img = '<a href="javascript:void(0);" onclick="javascript:add_file(this,'+id+')" id="on_'+id+'" class="on"><div class="icon"></div><img src="Public/images/ext/'+ext+'.png" width="80" alt="'+name+'" imgid="'+id+'" path="'+src+'"/></a>';
  13. }
  14. $('#thumbnails').append('<li><div id="attachment_'+id+'" class="img"></div></li>');
  15. $('#attachment_'+id).html(img);
  16. var datas='<div id="uplist_'+id+'"><input type="hidden" name="status" id="status" value="0"><input type="hidden" id="aids" name="aids[]" value="'+id+'" /><input type="text" id="filedata" name="filedata[]" value="'+src+'" /> <input type="text" id="namedata" name="namedata[]" value="'+name+'" /> &nbsp;<a href="javascript:remove_this(\'uplist_'+id+'\');">移除</a> </div>';
  17. $('#myuploadform').append(datas);
  18. ids.push(id);
  19. }
  20. function add_file(obj,id,status){
  21. var src = $(obj).children("img").attr("path");
  22. var name = $(obj).children("img").attr("alt");
  23. var filesize = $(obj).children("img").attr("imgsize");
  24. if($(obj).hasClass('on')){
  25. $(obj).removeClass("on");
  26. $('#myuploadform #uplist_'+id ).remove();
  27. for(var i=0;i<ids.length;i++){ if(ids[i]==id)ids.splice(i,1)}
  28. } else {
  29. var num = $('#myuploadform > div').length;
  30. if(num < file_limit){
  31. $(obj).addClass("on");
  32. var datas='<div id="uplist_'+id+'"><input type="hidden" name="status" id="status" value="'+status+'"><input type="hidden" id="aids" name="aids[]" value="'+id+'" /><input type="text" id="filedata" name="filedata[]" value="'+src+'" /> <input type="text" id="namedata" name="namedata[]" value="'+name+'" /> &nbsp;<a href="javascript:remove_this(\'uplist_'+id+'\');">移除</a> </div>';
  33. $('#myuploadform').append(datas);
  34. ids.push(id);
  35. }else{
  36. alert('已经达到附件限制数');
  37. }
  38. }
  39. }
  40. function uploadSuccess(file, serverData) {
  41. try {
  42. var progress = new FileProgress(file, this.customSettings.progressTarget);
  43. progress.setComplete();
  44. serverData = eval('('+serverData+')');
  45. if (serverData.status==1)
  46. {
  47. progress.setStatus(serverData.info);
  48. add_uploadok(serverData.data);
  49. }else{
  50. progress.setStatus(serverData.info);
  51. }
  52. //progress.setStatus("Complete.");
  53. progress.toggleCancel(false);
  54. } catch (ex) {
  55. this.debug(ex);
  56. }
  57. }
  58. function fileQueued(file) {
  59. try {
  60. //this.customSettings.tdFilesQueued.innerHTML = this.getStats().files_queued;
  61. var progress = new FileProgress(file, this.customSettings.progressTarget);
  62. progress.setStatus("等待上传...");
  63. progress.toggleCancel(true, this);
  64. } catch (ex) {
  65. this.debug(ex);
  66. }
  67. }
  68. function fileQueueError(file, errorCode, message) {
  69. try {
  70. if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
  71. alert("You have attempted to queue too many files.\n" + (message === 0 ? "You have reached the upload limit." : "You may select " + (message > 1 ? "up to " + message + " files." : "one file.")));
  72. return;
  73. }
  74. var progress = new FileProgress(file, this.customSettings.progressTarget);
  75. progress.setError();
  76. progress.toggleCancel(false);
  77. switch (errorCode) {
  78. case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
  79. progress.setStatus("File is too big.");
  80. this.debug("Error Code: File too big, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  81. break;
  82. case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
  83. progress.setStatus("Cannot upload Zero Byte files.");
  84. this.debug("Error Code: Zero byte file, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  85. break;
  86. case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
  87. progress.setStatus("Invalid File Type.");
  88. this.debug("Error Code: Invalid File Type, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  89. break;
  90. default:
  91. if (file !== null) {
  92. progress.setStatus("Unhandled Error");
  93. }
  94. this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  95. break;
  96. }
  97. } catch (ex) {
  98. this.debug(ex);
  99. }
  100. }
  101. function uploadStart(file) {
  102. try {
  103. /* I don't want to do any file validation or anything, I'll just update the UI and
  104. return true to indicate that the upload should start.
  105. It's important to update the UI here because in Linux no uploadProgress events are called. The best
  106. we can do is say we are uploading.
  107. */
  108. var progress = new FileProgress(file, this.customSettings.progressTarget);
  109. progress.setStatus("正在上传请稍后...");
  110. progress.toggleCancel(true, this);
  111. }
  112. catch (ex) {}
  113. return true;
  114. }
  115. function uploadProgress(file, bytesLoaded, bytesTotal) {
  116. try {
  117. var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
  118. var progress = new FileProgress(file, this.customSettings.progressTarget);
  119. progress.setProgress(percent);
  120. progress.setStatus("正在上传请稍后...");
  121. } catch (ex) {
  122. this.debug(ex);
  123. }
  124. }
  125. function uploadError(file, errorCode, message) {
  126. try {
  127. var progress = new FileProgress(file, this.customSettings.progressTarget);
  128. progress.setError();
  129. progress.toggleCancel(false);
  130. switch (errorCode) {
  131. case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
  132. progress.setStatus("上传错误: " + message);
  133. this.debug("Error Code: HTTP Error, File name: " + file.name + ", Message: " + message);
  134. break;
  135. case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
  136. progress.setStatus("上传错误.");
  137. this.debug("Error Code: Upload Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  138. break;
  139. case SWFUpload.UPLOAD_ERROR.IO_ERROR:
  140. progress.setStatus("服务器 I/O 错误");
  141. this.debug("Error Code: IO Error, File name: " + file.name + ", Message: " + message);
  142. break;
  143. case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
  144. progress.setStatus("服务器安全认证错误");
  145. this.debug("Error Code: Security Error, File name: " + file.name + ", Message: " + message);
  146. break;
  147. case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
  148. progress.setStatus("队列文件数量超过设定值.");
  149. this.debug("Error Code: Upload Limit Exceeded, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  150. break;
  151. case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
  152. progress.setStatus("附件安全检测失败,上传终止.");
  153. this.debug("Error Code: File Validation Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  154. break;
  155. case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
  156. progress.setStatus("己取消上传");
  157. progress.setCancelled();
  158. break;
  159. case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
  160. progress.setStatus("上传终止");
  161. break;
  162. default:
  163. progress.setStatus("Unhandled Error: " + errorCode);
  164. this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
  165. break;
  166. }
  167. } catch (ex) {
  168. this.debug(ex);
  169. }
  170. }
  171. // This event comes from the Queue Plugin
  172. function queueComplete(numFilesUploaded) {
  173. var status = document.getElementById("divStatus");
  174. status.innerHTML = numFilesUploaded + " file" + (numFilesUploaded === 1 ? "" : "s") + " uploaded.";
  175. }
  176. function uploadComplete(file) {
  177. if (this.getStats().files_queued > 0)
  178. {
  179. this.startUpload();
  180. }
  181. if (this.getStats().files_queued === 0) {
  182. document.getElementById(this.customSettings.cancelButtonId).disabled = true;
  183. }
  184. //this.customSettings.tdFilesQueued.innerHTML = this.getStats().files_queued-1;
  185. this.customSettings.tdFilesUploaded.innerHTML = this.getStats().successful_uploads+1;
  186. this.customSettings.tdErrors.innerHTML = this.getStats().upload_errors;
  187. }
  188. function fileDialogComplete(numFilesSelected, numFilesQueued) {
  189. this.customSettings.tdFilesQueued.innerHTML = this.getStats().files_queued;
  190. try {
  191. if (numFilesSelected > 0) {
  192. document.getElementById(this.customSettings.cancelButtonId).disabled = false;
  193. }
  194. /* I want auto start the upload and I can do that here */
  195. //this.startUpload();
  196. } catch (ex) {
  197. this.debug(ex);
  198. }
  199. }
  200. function preLoad() {
  201. if (!this.support.loading) {
  202. alert("You need the Flash Player 9.028 or above to use SWFUpload.");
  203. return false;
  204. }
  205. }
  206. function loadFailed() {
  207. alert("Something went wrong while loading SWFUpload. If this were a real application we'd clean up and then give you an alternative");
  208. }