FormData,ajaxSubmit,ajaxForm上传图片兼容
<html> <head>
<script src="/js/jquery-2.0.3.min.js"></script> <script src="/js/jquery.ajaxform.js"></script>
</head> <body>
<form name="form" action="upload" method="POST" id="userForm" enctype="multipart/form-data"> <div class="wrap"> <div class="album-old"> <div class="upload-btn btn-old"><input type="file" name="file" accept="image/*" id="image1" capture="camera"></div> <div class="upload-img "></div> </div> <div class="submit" style="margin-top: 200px;">提交</div> </div> </form>
</body>
<script type="text/javascript"> $(function(){ // $(".submit").on( // "click",function() { // var fileObj = document.getElementById("image1").files[0]; // var formData = new FormData(); // // formData.append("file",fileObj); // var xhr = new XMLHttpRequest(); // // xhr.open("post","/Api/News/upload",true); // xhr.send(formData); // // xhr.onload = function(e){ // if (this.status == 200) { // window.alert(this.responseText); // } // } // }) $(".submit").on("click",function() { userBrowser(); }); }); function userBrowser(){ var browserName=navigator.userAgent.toLowerCase(); if(/msie/i.test(browserName) && !/opera/.test(browserName)){ $("#userForm").ajaxSubmit(function(data){alert(data.fileurl);}); return ; }else if(/firefox/i.test(browserName)){ $("#userForm").ajaxSubmit(function(data){alert(data.fileurl);}); return ; }else if(/chrome/i.test(browserName) && /webkit/i.test(browserName) && /mozilla/i.test(browserName)){ $("#userForm").ajaxSubmit(function(data){alert(data.fileurl);}); return ; }else if(/opera/i.test(browserName)){ $("#userForm").ajaxSubmit(function(data){alert(data.fileurl);}); return ; }else if(/webkit/i.test(browserName) &&!(/chrome/i.test(browserName) && /webkit/i.test(browserName) && /mozilla/i.test(browserName))){ $("#userForm").ajaxSubmit(function(data){alert(data.fileurl);}); return ; }else{ $("#userForm").ajaxSubmit(function(data){alert(data.fileurl);}); } } function test() { alert(navigator.userAgent.toLowerCase().indexOf("MSIE")); if(navigator.userAgent.indexOf("MSIE")>0) $("#userForm").ajaxSubmit(function(data){alert(data);}); else $("#userForm").ajaxForm(function(data){alert(data);}); }
</html> 注:FormData IE8兼容问题
相关推荐
adgjlxxx 2016-12-23
wuxhyou 2015-04-11
RanQi0 2016-04-21
sunnyishere 2015-12-21
roodyszz0 2014-03-06
欧阳凯少 2012-11-16
sunnyishere 2012-05-29
WillZhang 2011-10-24
zhuchengzzcc 2011-09-08
chongxiaocheng 2018-10-14
ppsurcao 2016-12-29
urbunt 2015-08-17
DeadRabbit0 2016-09-25
WildestDeram 2019-04-26
年轻正好 2016-01-23
bobobola 2016-01-04
blogfromzkp 2012-03-01
读立写生 2018-05-12
81751330 2015-03-10