文件上传submitajax方式
Posted 默默红尘
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件上传submitajax方式相关的知识,希望对你有一定的参考价值。
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>
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>
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>
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>
以上是关于文件上传submitajax方式的主要内容,如果未能解决你的问题,请参考以下文章
Alamofire 文件上传出现错误“JSON 文本未以数组或对象开头,并且允许未设置片段的选项”