js讀取JSON文件獲取數據


  <div id='search'> 
    <el-upload 
      :multiple="false"
      :show-file-list="false"
      //:on-success="handleImageSuccess" //上傳成功後出發
      :before-upload="handleBeforeUpload"//上傳前鉤子函數
      accept=".json"
      class="image-uploader" 
      action="上傳文件路徑"
    > 
      <div class="el-upload__text">
        <el-button type="primary">點擊上傳</el-button> 
      </div>
    </el-upload>
  </div>
//上傳前鉤子函數
 handleBeforeUpload(file) {
    let url = null;
    if(window.createObjectURL !== undefined) { // basic
        url = window.createObjectURL(file);
    } else if(window.URL !== undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file);
    } else if(window.webkitURL !== undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file);
    }
    //利用Axios讀取JSON數據
    this.$axios.get(url).then(res=>{
      console.log(res)//讀取文件內容
    }).catch(err=>{
		throw err
	})
} 

返回結果
返回數據

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章