Vue實現(xiàn)讀取本地圖片的示例代碼
制作一個簡單的展示頁面
需要讀取本地靜態(tài)文件獲取文字信息 和確定圖片路勁
剛開始都是使用new XMLHttpRequest()。本地測試都沒有問題。但是部署到云服務器時 對圖片的判斷就失效了 無論請求地址多少 返回響應碼都是200.(沒有找到原因)。 所以更換了方式 使用img的加載失敗回調方法。
url中文編碼問題(也是本地測試沒問題,線上就必須轉碼了)decodeURI()
// app.vue <template> <div id="app"> <div class="header"> <div class="main"> <div class="drug-name"> <span>{{drugName}}</span> </div> <div class="drug-img"> <img :src="drugImg" :alt="drugName" @error="imageLoadError"> </div> <div class="drug-info" v-html="drugInfo"></div> </div> </div> </div> </template> <script > export default { components: { }, created() { this.init() this.OpenPup(); // this.imgChange(); console.log(this.$route.query) }, data(){ return { drugName: '', drugInfo: '', drugImg: '', imgCheckCount: 0 } }, methods:{ init(){ this.drugName=decodeURI(this.$route.query.drugName) // this.drugName=decodeURI(this.$route.query.drugName) console.log(this.$route.query.drugName) // var destr = UrlDecode(str); // this.imgChange() this.drugImg = 'image/'+this.drugName+'.jpg' }, imageLoadError(e){ console.log("onerror") // this.drugImg = 'image/'+this.drugName+'.JPG' // this.loadingOnOff = true this.imgCheckCount += 1 this.imgCheks() e.target.onerror = null }, imgCheks(){ if(this.imgCheckCount === 1){ this.drugImg = 'image/'+this.drugName+'.JPG' }else if(this.imgCheckCount === 2){ this.drugImg = 'image/'+this.drugName+'1.jpg' }else if(this.imgCheckCount === 3){ this.drugImg = 'image/'+this.drugName+'1.JPG' } }, imgChange(){ // console.log("ceshi"+this.drugName) // console.log("ceshi"+this.isFileExist('image/'+this.drugName+'.jpg')) // console.log("ceshi"+this.isFileExist('image/'+this.drugName+'1.jpg')) if(this.checkImg('image/'+this.drugName+'.jpg')){ this.drugImg = 'image/'+this.drugName+'.jpg' }else if(this.checkImg('image/'+this.drugName+'.JPG')){ this.drugImg = 'image/'+this.drugName+'.JPG' }else if(this.checkImg('image/'+this.drugName+'1.jpg')){ this.drugImg = 'image/'+this.drugName+'1.jpg' }else if(this.checkImg('image/'+this.drugName+'1.JPG')){ this.drugImg = 'image/'+this.drugName+'1.JPG' } // console.log(this.drugImg) // console.log(this.isFileExist('image/'+this.drugName+'.jpg')) }, OpenPup(){ let xhr = new XMLHttpRequest() // let okStatus = document.location.protocol === "file:" ? 0 : 200; // xhr.open("GET", val.url, false); // 線上鏈接地址 // let drugUrl = this.drugName+".text" let drugUrl = 'drug/'+this.drugName+".txt" xhr.open("GET", drugUrl, false); // public文件夾下的絕對路徑 xhr.overrideMimeType("txt/html;charset=utf-8") xhr.send(null) // console.log(xhr.responseText) // xhr.responseText為文本中的內容 this.drugInfo = xhr.responseText // console.log(this.drugInfo) }, // // 判斷本地是否有該文件 // showimg (url) { // // 判斷結果 // let result = false // url = './' + url // // 獲取該目錄下所有文件名 // const files = require.context('public/image/', false, /\.jpg$/) // console.log(files.keys()) // const filedata = files.keys() // // 判斷是否有該文件 // filedata.forEach((item) => { // if (item === url) { // result = true // } // }) // // 返回結果 // return result // }, isFileExist(filePath) { let xhr = new XMLHttpRequest(); xhr.open('GET', filePath, false); xhr.send(); console.log(xhr) return xhr.status === 200; }, checkImg(imgurl) { var imgObj = new Image(); imgObj.src = imgurl; imgObj.onload = function (res) { return true; console.log('有效鏈接') } imgObj.onerror = function (err) { return false; console.log('無效鏈接') } } } }; </script> <style lang="less"> //#app { // font-family: Avenir, Helvetica, Arial, sans-serif; // -webkit-font-smoothing: antialiased; // -moz-osx-font-smoothing: grayscale; // text-align: center; // color: #2c3e50; //} html,body{ width: 100%; height: 100%; overflow: scroll;} html::-webkit-scrollbar, body::-webkit-scrollbar{width:0px;height:0px;} body{ margin: 0; padding: 0; } #app{ width: 100vw; margin: 0; padding: 0; } .header { /*line-height: 1.5;*/ /*max-height: 100vh;*/ margin: 0; padding: 0; width: 100vw; background-color: blanchedalmond; } .main{ margin: 0 5vw; width: 90vw; min-height: 100vh; background-color: white; } .drug-name{ margin-bottom: 10vw; padding-top: 10vw; text-align:center } .drug-name span{ /*margin: auto;*/ /*margin-left: 50px;*/ color: #181818; font-size: 32px; font-weight: bold; display: inline-block; margin: 0 auto; text-align: center; } .drug-img { text-align:center } .drug-img img{ max-width: 80vw; height: auto; width: auto; margin: auto; text-align:center } .drug-info { margin: 5vw 5vw 0; padding-bottom: 5vw; font-size: 16px; color: #181818; line-height: 30px; overflow: auto; } .drug-info span{ font-size: 16px; color: #181818; } h1 { font-size: 20px; text-align: left; margin-bottom: 10px; } ::v-deep p{ margin-bottom: 15px; } ::v-deep b { font-size: 19px; font-weight: bold; /*margin-bottom: 15px;*/ /*padding-bottom: 15px;*/ text-indent:2em; } </style>
以上就是Vue實現(xiàn)讀取本地圖片的示例代碼的詳細內容,更多關于Vue讀取本地圖片的資料請關注腳本之家其它相關文章!
相關文章
Vue3+Element-Plus實現(xiàn)左側菜單折疊與展開功能示例
本文主要介紹了Vue3+Element-Plus實現(xiàn)左側菜單折疊與展開功能示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04vue項目中chunk-vendors.js提示報錯的查看方法(最新推薦)
在vue項目中,chunk-vendors.js報出的錯誤提示經常會導致開發(fā)者困惑,正確查看錯誤的方法是從錯誤提示的詳細信息中找到報錯原因,下面給大家分享vue項目中chunk-vendors.js提示報錯的查看方法,感興趣的朋友一起看看吧2024-12-12從Vuex中取出數(shù)組賦值給新的數(shù)組,新數(shù)組push時報錯的解決方法
今天小編就為大家分享一篇從Vuex中取出數(shù)組賦值給新的數(shù)組,新數(shù)組push時報錯的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09關于vue中路由的跳轉和參數(shù)傳遞,參數(shù)獲取
這篇文章主要介紹了關于vue中路由的跳轉和參數(shù)傳遞,參數(shù)獲取方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03