vue excel文件上傳

不能老盯着手機屏幕,要不時地擡起頭,看看老闆的位置。

  1. 技術背景:vue上傳excel文件給後端時選擇了fromdata,
  2. 安裝axios:npm i axios -D
  3. FormData:FormData類型實際上是在XMLHttpRequest2級定義的,它是爲序列化表以及建立與表單格式相同的數據(固然是用於XHR傳輸)提供便利。
  • 一、建立一個空對象實例:let fileFormData = new FormData();
  • 二、添加數據:咱們能夠經過append(key,value)來添加數據,若是指定的key不存在則會新增一條數據,若是key存在,則添加到數據的末尾fileFormData.append('File',this.initFile);
  1. 設置Content-Type:let requestConfig = {headers: {"Content-Type": "multipart/form-data"}};
  2. 建立axios實例並添加:withCredentials:true表示跨域請求時是否須要使用憑證. 默認爲false
  3. 發送axios請求:參數(後臺接口地址,fileFormData,requestConfig)

完整代碼

//點擊發送事件
preserveCompanyName() {
      import axios from 'axios' //先下載axios (npm i axios -D)!!!(不在這裏寫,而後在export default 外引入)
      let fileFormData = new FormData();
      fileFormData.append('File',this.initFile);
      let requestConfig = {
        headers: {
          "Content-Type": "multipart/form-data"
        }
      };

      const instance = axios.create({
        withCredentials: true
      })

      instance.post('http://192.168.1.48:8000/basicTable/upload_file',fileFormData,requestConfig ).then(res=>{
        this.$message.success('上傳成功!')
      })

      this.dialogVisible1 = false;
}
複製代碼

至此 完結!best regards!vue

相關文章
相關標籤/搜索