upload.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var filterTypes="*";
  2. function seeFile(path,id,file){
  3. //alert("a");
  4. //window.open("SeeFileServlet.do?path=" + path + "&id="+ id + "&fileName=" + file);
  5. popup("SeeFileServlet.do?path=" + path + "&id="+ id + "&fileName=" + file,'seefile');
  6. }
  7. function modify(index,file){
  8. if ($("#modify"+index).text()=="修改"){
  9. $("#"+index+"1").html("<input id=\""+"file"+index +"1"+ "\"" + " value=\""+$("#"+index+"1").html()+"\""+">");
  10. $("#"+index+"3").html("<select name=\"" + "file"+index +"3" + "\"" + " id=\""+ "file"+index +"3" + "\"" + "><option value=0>是</option><option value=1>否</option></select>");
  11. $("#file"+index +"3").val($("#"+index+"3").html());
  12. $("#modify"+index).text("提交");
  13. }
  14. else
  15. {
  16. //alert(file);
  17. //alert($("#"+"file"+index+"1").val());
  18. $.ajax({
  19. type:"POST",
  20. url:"AjaxModifyFile.do",
  21. data:{
  22. id:$("#file-id").val(),
  23. path:$("#file-path").val(),
  24. fileName:encodeURIComponent($("#"+"file"+index+"1").val()),
  25. fileType:$("#"+"file"+index+"3").val(),
  26. fileId:file},
  27. success:function(data){
  28. if ((data.substring(0,3))=="err")
  29. $("#errMsg").html(data);
  30. else
  31. $("#output1").html(data);
  32. }
  33. })
  34. }
  35. }
  36. function submitFile(index){
  37. }
  38. function remove(file){
  39. //alert($("#id").val());
  40. //alert($("#path").val());
  41. //alert(file);
  42. $.ajax({
  43. type:"POST",
  44. url:"AjaxRemoveFile.do",
  45. data:{id:$("#file-id").val(),path:$("#file-path").val(),file:encodeURIComponent(file)},
  46. success:function(data){
  47. if ((data.substring(0,3))=="err")
  48. $("#errMsg").html(data);
  49. else
  50. $("#output1").html(data);
  51. }
  52. })
  53. }
  54. $(document).ready(function() {
  55. var options = {
  56. //target: '#output1', // 用服务器返回的数据 更新 id为output1的内容.
  57. beforeSubmit: showRequest, // 提交前
  58. success: showResponse, // 提交后
  59. //另外的一些属性:
  60. //url: url // 默认是form的action,如果写的话,会覆盖from的action.
  61. //type: type // 默认是form的method,如果写的话,会覆盖from的method.('get' or 'post').
  62. //dataType: null // 'xml', 'script', or 'json' (接受服务端返回的类型.)
  63. //clearForm: true // 成功提交后,清除所有的表单元素的值.
  64. resetForm: true // 成功提交后,重置所有的表单元素的值.
  65. //由于某种原因,提交陷入无限等待之中,timeout参数就是用来限制请求的时间,
  66. //当请求大于3秒后,跳出请求.
  67. //timeout: 3000
  68. };
  69. //alert("=============") ;
  70. //'ajaxForm' 方式的表单 .
  71. $('#attach-form').ajaxForm(options);
  72. //或者 'ajaxSubmit' 方式的提交.
  73. //$('#myForm').submit(function() {
  74. // $(this).ajaxSubmit(options);
  75. // return false; //来阻止浏览器提交.
  76. //});
  77. });
  78. // 提交前
  79. function showRequest(formData, jqForm, options) {
  80. //alert("=====");
  81. // formdata是数组对象,在这里,我们使用$.param()方法把他转化为字符串.
  82. var queryString = $.param(formData); //组装数据,插件会自动提交数据
  83. //alert(queryString); //类似 : name=1&add=2
  84. $name=$('#attach-form-file').val();
  85. if ($name=="")
  86. {
  87. alert("请选择要上传的文件");
  88. return false;
  89. }
  90. else
  91. {
  92. var len=$name.length;
  93. var start=$name.lastIndexOf(".");
  94. var type=$name.substring(start,len);
  95. if (filterTypes!="*"){
  96. if (filterTypes.indexOf(type)==-1)
  97. {
  98. alert("文件格式"+filterTypes);
  99. return false;
  100. }
  101. }
  102. if (type==".exe" || type==".jsp" || type==".bat" ||type==".jar"){
  103. alert("不允许的文件格式");
  104. return false;
  105. }
  106. $("#errMsg").html("正在上传...");
  107. return true;
  108. }
  109. }
  110. // 提交后
  111. function showResponse(responseText, statusText) {
  112. if ((responseText.substring(0,3))=="err")
  113. $("#errMsg").html(responseText);
  114. else
  115. {
  116. $("#output1").html(responseText);
  117. $("#errMsg").html("正在上传...OK");
  118. }
  119. }