upload.jsp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <%@ page pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  7. <html>
  8. <head>
  9. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  10. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  11. <link rel="stylesheet" href="<%=basePath %>resource/ext/resources/css/ext-all-gray.css" type="text/css"></link>
  12. <link rel="stylesheet" href="<%=basePath %>resource/css/main.css" type="text/css"></link>
  13. <style>
  14. .btn{ background-color:#FFF; border:1px solid #CDCDCD;height:24px; width:70px;}
  15. </style>
  16. <script type="text/javascript" src="<%=basePath %>resource/ext/ext-all.js"></script>
  17. <script type="text/javascript">
  18. var i=0;
  19. //用来动态生成span,upfile的id
  20. function addAttachmentToList(){
  21. var count=null;
  22. var o=G('attachmentList').childNodes;
  23. if(G('btnAdd').value=='添加文件'&&o.length>0){
  24. G('attachmentList').removeChild(o[0]);
  25. }
  26. if(findAttachment(event.srcElement.value))
  27. return;
  28. //如果此文档已在附件列表中则不再添加 //动态创建附件信息栏并添加到附件列表中
  29. var span=document.createElement('span');
  30. span.id='_attachment'+i;
  31. //span.innerHTML='"<input type="file" name="upload'+i+'"id="fileupload'+i+'" value="'++'">"'+extractFileName(event.srcElement.value)+'&nbsp;<a href="javascript:delAttachment('+(i++)+')"><font color="blue">删除</font></a><font id="sendMsg'+i+'"></font><br/>';
  32. span.innerHTML=extractFileName(event.srcElement.value)+'&nbsp;<a href="javascript:delAttachment('+(i++)+')"><font color="blue">删除</font></a><font id="sendMsg'+i+'"></font><br/>';
  33. span.title=event.srcElement.value;
  34. G('attachmentList').appendChild(span);
  35. send(event.srcElement.value,i);
  36. //显示附件列表并变换添加附件按钮文本
  37. if(G('attachmentList').style.display=='none'){
  38. G('btnAdd').value='继续添加';
  39. G('attachmentList').style.display='';
  40. G('btnClear').style.display='';
  41. }
  42. G('total').innerText='当前选择上传'+G('attachmentList').childNodes.length+'个附件';
  43. }
  44. function selectAttachment() {
  45. //先清除无效动态生成的多余upfile
  46. cleanInvalidUpfile();
  47. //动态创建上传控件并与span对应
  48. var upfile='<input type="file" style="display:none" name="upload'+i+'" onchange="addAttachmentToList();"id="_upfile'+i+'">';
  49. document.body.insertAdjacentHTML('beforeEnd',upfile);
  50. G('_upfile'+i).click();
  51. }
  52. function extractFileName(fn){
  53. return fn.substr(fn.lastIndexOf('//') + 1);
  54. }
  55. function findAttachment(fn){
  56. var o=G('attachmentList').getElementsByTagName('span');
  57. for(var i=0;i<o.length;i++)
  58. if(o[i].title==fn)
  59. return true;
  60. return false;
  61. }
  62. function delAttachment(id) {
  63. G('attachmentList').removeChild(G('_attachment'+id));
  64. document.body.removeChild(G('_upfile'+id));
  65. //当附件列表为空则不显示并且变化添加附件按钮文本
  66. if(G('attachmentList').childNodes.length==0){
  67. G('btnAdd').value='添加附件';
  68. G('attachmentList').style.display='none';
  69. G('btnClear').style.display='none'; }
  70. G('total').innerText='当前选择上传'+G('attachmentList').childNodes.length+'个附件';
  71. }
  72. function cleanInvalidUpfile(){
  73. var o=document.body.getElementsByTagName('input');
  74. for(var i=o.length-1;i>=0;i--)
  75. if(o[i].type=='file'&&o[i].id.indexOf('_upfile')==0){
  76. if(!G('_attachment'+o[i].id.substr(7)))
  77. document.body.removeChild(o[i]);
  78. }
  79. }
  80. function clearAttachment(){
  81. var o=G('attachmentList').childNodes;
  82. for(var i=o.length-1;i>=0;i--)
  83. G('attachmentList').removeChild(o[i]);
  84. o=document.body.getElementsByTagName('input');
  85. for(var i=o.length-1;i>=0;i--)
  86. if(o[i].type=='file'&&o[i].id.indexOf('_upfile')==0){
  87. document.body.removeChild(o[i]);
  88. }
  89. G('btnAdd').value='添加文件';
  90. G('attachmentList').style.display='none';
  91. G('btnClear').style.display='none';
  92. G('total').innerText='当前选择上传0个附件';
  93. }
  94. function getAttachmentInfo(){
  95. //已知的js获取本地文件大小的三种方式
  96. //1.通过FSO2.通过ActiveX3.通过Flash(设置可能更麻烦)与js交互
  97. //注:QQ邮箱中获取本地文件大小就是采用第二种方式
  98. }
  99. function G(id){
  100. return document.getElementById(id);
  101. }
  102. function send(file,index){
  103. Ext.Ajax.request({
  104. url :'fileUpload.action',
  105. params :{
  106. file : file
  107. },
  108. method : 'post',
  109. callback : function(options,success,response){
  110. document.getElementById('sendMsg'+index).innerHTML = "文件上传成功";
  111. var f = parent.document.getElementById('sendFiles');
  112. f.innerHTML = f.innerHTML + '&nbsp;&nbsp;' + file.substring(file.lastIndexOf('\\')+1);
  113. }
  114. });
  115. }
  116. </script>
  117. <body>
  118. <form id="form">
  119. <fieldset style="border:1pxsolid#84A24A;text-align:left;COLOR:#84A24A;FONT-SIZE: 12px;font-family:Verdana;padding:5px;">
  120. <legend></legend>
  121. <input type="button" class='btn' value="添加文件"id="btnAdd"onclick="selectAttachment();">&nbsp;
  122. <input type="button" class='btn' value="清空文件"id="btnClear"style="display:none"onclick="clearAttachment();">
  123. <div id="attachmentList"style="margin:3px0px0px0px;padding:4px3px4px3px;background-color:#DEEBC6;display:none;border:1pxsolid#84A24A;"> </div>
  124. <div id="total"style="margin:3px0px0px0px;">当前选择上传0个文件</div> </fieldset>
  125. <!-- //<input type="submit" name="submit" > -->
  126. </form>
  127. </body>
  128. </html>