1、【Vue上传文件】当接口Content-Type为multipart/form-data时,如何上传文件到后端

在这里插入图片描述

<input type="file" id="putfile" ref="inputer" @change="fileUpload" />
//方法:
fileUpload(event) {
     
      console.log(event);
      let file = event.target.files;//一个文件流
      let formData = new FormData();
      formData.append("file", file[0]);
      console.log(formData);
      // 文件上传
      this.$axios({
     
        method: "POST",
        url: "/api/device/deviceInfo/createDeviceInfoByExcel",
        headers: {
     
          "Content-Type": "multipart/form-data",
          token: localStorage.getItem("token"),//token值,根据接口要求传或者不传
        },
        data: formData,
      })
        .then((res) => {
     
          console.log(res);
        })
        .catch((e) => {
     
          alert(e);
        });
    },

你可能感兴趣的:(vue,vue,javascript)