先引入解析包npm install shpjs --save
然后使用element_ui的文件上傳組件
<el-upload drag class="fileChoice" :auto-upload=false :show-file-list="false" action="" :on-change="parsingShape" > <img type="file" src="./theme/img/Group 8 Copy 3.png" alt @click="parsingShape"/> </el-upload>
img為自定義的小圖標,隨意換。:auto-upload設置為false不上傳。:on-change="parsingShape"為文件上傳之后的回調
parsingShape(files, fileList){ let _self=this; if(fileList){ this.file=fileList[fileList.length-1] const name=this.file.name const extension=name.split('.')[1] if('zip'!==extension){ this.$message.warning(this.$t('common.message.isNotFile')); }else { const reader=new FileReader() const fileData=this.file.raw reader.readAsArrayBuffer(fileData) reader.onload = function(e){ shp(this.result).then( function(data){ console.log(data) }).catch(function(){ _self.$message.warning(_self.$t('common.message.fileFormat')); }); } } } }
以上是js代碼,可獲得最后解析之后的geoJson數據。
注意:此方法只能解析zip文件。以下圖片問zip壓縮包里的文件