jQuery-File-Upload兼容IE8的问题:data.submit()没有发送请求
這段代碼來自我的提問以及后來的回答。
https://segmentfault.com/q/1010000009635544
先給出此問題相關的代碼:下面的代碼,我已經成功兼容了IE9+,但是沒能成功兼容IE8
先給出此問題相關的代碼:
下面的代碼,我已經成功兼容了IE9+,但是沒能成功IE8,
這段的代碼不需要關注。
$("#file-upload").fileupload({ url: "/api/org/importOrg",add: function(e, data) {var file = data.files[0];$("#fileInput").val(file.name);$("#importSuccess").unbind().bind('click', function() {if ($("#fileInput").val() === "") {Messenger().post({message: "請先上傳文件!",type: 'info',showCloseButton: true});return;}if (browser == "Microsoft Internet Explorer" && (trim_Version == "MSIE7.0" )) {Messenger().post({message: '瀏覽器版本過老,不支持導入功能',type: 'info',showCloseButton: true});return;} else if (!/.xls$|.xlsx$/.test(file.name)) {Messenger().post({message: '請上傳以.xls/.xlsx為后綴名的正確Excel模版文件',type: 'info',showCloseButton: true});return;} else if (file.size >= 10485760) {//上傳文件大小不能超過10MbMessenger().post({message: '上傳的文件大小不能超過10Mb',type: 'info',showCloseButton: true});return;}$('#importSuccess').showLoading({'overlayWidth': $('#importSuccess').innerWidth(),'overlayHeight': $('#importSuccess').innerHeight()});//var pNode = pNodeSelectTree.getId();//$("#file-upload").fileupload({formData: {name: $("#fileInput").val(), //type:$("[name=userType]:checked").val() }});$("#file-upload").fileupload({formData: {name: $("#fileInput").val()}});console.log('before submit:'+ $("#fileInput").val());//before submit:組織導入模板.xls//$("#file-upload").fileupload({url:"/api/org/"+pNode[0]+"/importOrg"});data.submit();console.log("after submit");//after submit});},done: function(e, rep) {console.log("done");//沒有觸發fail,沒觸發done回掉var myResult=JSON.parse(rep.result);//后端返回字符串,解析成JSON對象,請求的content-type應該為text/plain,避免IE9下返回application/json提示下載,從而兼容IE9// myResult={"failed":1,"succeed":10,"fails":[{"line":15,"name":"的薩芬","orgName":"組織","mobile":1352222222,"error":"出錯啦!!!"},{"line":15,"name":"的薩芬","orgName":"組織","mobile":1352222222,"error":"出錯啦!!!"},{"line":15,"name":"的薩芬","orgName":"組織","mobile":1352222222,"error":"出錯啦!!!"}]};$('#importSuccess').hideLoading();$("#fileInput").val('');if (myResult.failed == 0) {new Modal({icon: "success",title: "導入成功",content: "成功導入" + myResult.succeed + "條數據",isConfirm: false}).show(function() {});} else {$('#importErrorModal').html(importErrorModal(myResult));new Modal('#importErrorModal').show();$('#importErrorModal td>div').each(function(){this.scrollWidth > this.offsetWidth && $(this).tooltip();});$('#importErrorModal .modal-header').moveAnimate({modalHeaders:'#importErrorModal .modal-header'});}},fail: function() {console.log("fail");//沒有打印,也就是說沒回調fail$('#importSuccess').hideLoading();$("#fileInput").val('');}});我遇到的問題不是所謂的返回JSON數據,IE瀏覽器提示下載的問題,這個問題我已經解決了。
現在的問題是,在IE8下,此段程序無法回調done和fail函數,但是在IE9+瀏覽器和其他主流瀏覽器中是可行的。
根據上面那段程序的打印結果,說明add函數是成功執行的。
我也監控了network的通信,只有loading.gif這個表明正在加載的通信,沒有其他相關的請求&&回復。
這也佐證了done和fail函數沒有被回調,那么問題是什么呢?
我的回答:
IE9的兼容問題我已經在昨天嘗試了一天被我解決,但是之后IE8的兼容問題一直沒有被解決,雖然我也花了近一天的時間,在stack overflow上搜索相關問題,但是沒有什么收獲。
我是題主。我在這個問題上花了兩個白天的時間,終于解決了這個問題。
之所以能解決這個問題,是因為我重新檢查了前人寫的代碼邏輯。這個問題其實和HTML代碼緊密相關,我之前只注意JS代碼。
- HTML代碼
我們可以看到,這個其實是通過click觸發click。
這個是很常見的手法,因為很難看,也不好定制(至少我不知道如何定制它的CSS)。所以通過隱藏input,通過button調用input成為大多數人的選擇。
但是IE8出于安全性,不允許這么做,所以input看似被調用,但是沒有獲取到data, 下面這段英文來自微軟關于IE的文檔:
IE doesn’t allow manipulation of the type=”file” input element from javascript due to security reasons. Setting the filename or invoking a click event to show the browser dialog will result in an “Access is denied” error on the form submit.
這樣如何保證安全性,我就不知道了。
所以為了避免這個限制,對HTML代碼進行改動:看似在點擊button,實則在點擊
input
SASS
.uploadFile{span{position: relative;display: inline-block;}#file-upload { // 設置占據空間為0,看似點擊button,實則在點擊file-upload,從而避開IE8處于安全限制禁止間接點擊input type=file的bugposition: absolute;width: 100%;height: 100%;//和父元素同高寬top: 0;right: 0;opacity: 0;filter: alpha(opacity=0);}//解決此bug的方法詳見http://wenzhixin.net.cn/2014/07/30/ie8_file_problem}這是我看了這篇博客:http://wenzhixin.net.cn/2014/07/30/ie8_file_problem 后所做的嘗試,然后works
我的感想:通過調試確定問題的根源,再根據問題在谷歌上搜答案。
總結
以上是生活随笔為你收集整理的jQuery-File-Upload兼容IE8的问题:data.submit()没有发送请求的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ghost手动恢复linux,GHOST
- 下一篇: Bilinear Pairing双线性配