码迷,mamicode.com
首页 > Web开发 > 详细

文件上传submit、ajax方式

时间:2017-08-11 17:23:15      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:table   none   gif   put   open   filename   class   script   value   

submit方式:

技术分享
 1 <form id="postForm" name="postForm" action="${rc.contextPath}/backdoor/uploadGroovy/upload"
 2       enctype="multipart/form-data" method="post">
 3     <table style="font-size:14px;" class="tablelist" align="center">
 4         <th>
 5             选择上传的zip:
 6         </th>
 7         <th><input id="file" type="file" name="file">
 8         </th>
 9         <th>
10             <div>
11                 <button type="button" onclick="uploadFile()">上传</button>
12             </div>
13         </th>
14     </table>
15 </form>
View Code

 

技术分享
 1 <script type="text/javascript">
 2     function uploadFile() {
 3         var file = document.getElementById("file").value;
 4         if (file == ‘‘) {
 5             alert(请选择上传的文件!);
 6             return;
 7         }
 8         document.getElementById("postForm").submit();
 9     }
10 </script>
View Code

 

ajax方式:

技术分享
 1 <form id="postForm" name="postForm" action="${rc.contextPath}/backdoor/uploadGroovy/upload"
 2       enctype="multipart/form-data" method="post">
 3     <table style="font-size:14px;" class="tablelist" align="center">
 4         <th>
 5             选择上传的zip:
 6         </th>
 7         <th><input id="file" type="file" name="file">
 8         </th>
 9         <th>
10             <div>
11                 <button type="button" onclick="uploadFile()">上传</button>
12             </div>
13         </th>
14     </table>
15 </form>
View Code

 

技术分享
 1 <script type="text/javascript">
 2     function uploadFile() {
 3         if (confirm("您确定要上传吗!")) {
 4             var file = document.getElementById("file").value;
 5             if (file == ‘‘) {
 6                 alert(请选择上传的文件!);
 7                 return;
 8             }
 9             var fileName = $("#file").val();
10             var fileTypes = new Array("zip");  //定义可支持的文件类型数组
11             var fileTypeFlag = "0";
12             var newFileName = fileName.split(.);
13             newFileName = newFileName[newFileName.length - 1];
14             for (var i = 0; i < fileTypes.length; i++) {
15                 if (fileTypes[i] == newFileName) {
16                     fileTypeFlag = "1";
17                 }
18             }
19             if (fileTypeFlag == "0") {
20                 alert("上传文件必须是zip格式!");
21                 return;
22             }
23             $.ajax({
24                 url: "${rc.contextPath}/backdoor/uploadGroovy/upload",
25                 type: "post",
26                 data: new FormData($(#postForm)[0]),
27                 processData: false,
28                 contentType: false,
29                 success: function (data) {
30                     if (data.code == 1) {
31                         window.location.href = "${rc.contextPath}/backdoor/deploy/queryDeploy";
32                     } else {
33                         alert(data.message);
34                     }
35                 }
36             });
37         }
38     }
39 </script>
View Code

 

文件上传submit、ajax方式

标签:table   none   gif   put   open   filename   class   script   value   

原文地址:http://www.cnblogs.com/hunt/p/7346861.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!