1. 程式人生 > >axios在vue中的應用(二)—— 表單提交上傳圖片

axios在vue中的應用(二)—— 表單提交上傳圖片

使用axios實現圖片預覽、圖片上傳等功能:

methods: {
    // 圖片預覽
    priviewImg(e) {
        // ...
    },
    // 提交表單
    sumitRefund() {
        let fd = new FormData();
        fd.append("reason", this.refundReason);
        fd.append("money", this.refundMoney);
        fd.append("order", this.refundOrder);
        fd.append("pic", this.sendImg);
        // 設定請求頭
        let config = {
             headers: {
                'Content-Type': 'multipart/form-data'  //以表單傳資料的格式來傳遞data
            }
        };
       this.$http.post("service/refund", fd, config)
          .then(res => {
              console.log(res);
          })
          .catch(err => {
              console.log(err);
          });
      }
  }