問題: IE9中無法使用FormData
思路
坑
問題解決
項目使用VUE編寫,UI是ElementUI,但是Element官方明確了upload方面是不支持IE9的,看源碼的意思是已經擱置了iframe上傳的后續開發
改用vue-upload-component 作者對IE9專門做了兼容,就是使用起來理解成本有點兒高
如何觸發上傳
通過ref獲取upload實例,在添加文件時 激活上傳
this.$refs.upload.active = true
如何判斷當前上傳的狀態(添加,更新,刪除,上傳成功,上傳失敗)
每次上傳的狀態變化時 都會調用@input-file綁定的回調,形參是newFile, oldFile,通過新舊文件的對比來得到當前的狀態,感覺有點兒反策略模式的意思,自己通過元狀態的組合來得到當前狀態,習慣的話覺得還是挺有意思的
inputFile(newFile, oldFile) { // 舊文件活躍 新文件不活躍 此時上傳過程完成 if (newFile && oldFile && !newFile.active && oldFile.active) { this.$refs.upload.active = false // 獲得相應數據 let res = '{}' // 此處判斷相對簡單,可以參考jquery.form.js中做的判斷 if (/<pre/.test(newFile.response)) { res = />(.*)</.exec(newFile.response)[1] } res = JSON.parse(res) if (res.code !== 200) { if (res.code === 402) { this.$route.push('/login') return } Message.error(res.message) } else { Message.success('上傳成功') // 回顯圖片 this.upload.url = res.data.url.replace(/&/g, '&') } if (newFile.xhr) { // 獲得響應狀態碼 console.log('status', newFile.xhr.status) } } // 添加文件 if (newFile && !oldFile) { this.$refs['upload' + this.index].active = true }}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答