vue axios FormData 前端上传文件

  • vue 使用原生input + FormData上传文件
  • 不用表单提交,form表单提交页面会跳转
   <input type="file" ref="fileInput" />
   <button @click="upload">上传button>
 upload() {
      const files = this.$refs.fileInput.files
      var formData = new FormData();
      let config = {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      };
      formData.append("file", files[0]);
      this.axios.post("/api/file/upload", formData, config).then((response) => {
        this.$message.success(response.data);
      });
    },

你可能感兴趣的:(vue,前端,html,vue.js,html5)