vue文件上传组件,使用原生input file实现

6 篇文章 0 订阅
4 篇文章 0 订阅

vue 实现输入框右侧icon文件夹点击上传图片组件
点击右侧文件夹实现图片上传
a. 调用图片上传父组件
formData 就获取 图片上传组件传过来的地址。

// html

<upload-image v-model="formData" />

b. 图片上传组件

// html 
<template>
  <div>
    <el-input
      clearable
      v-model.trim="uploadImgUrl"
      size="mini"
      @change="changeInput"
    >
      <template slot="append">
        <i class="iconfont iconfolder-o"></i>
        <input type="file" class="file" ref="files" @change="getImages" />
      </template>
    </el-input>
  </div>
</template>

// js
import axios from "axios";
import { getToken } from "@/utils/auth";
export default {
  model: {
    prop: "value",
    event: "input"
  },
  props: {
    value: {
      type: "",
      default: ""
    }
  },
  data() {
    return {
      requestUrl: process.env.BASE_API + "/file/upload",
      headers: {
        Authorization: getToken()
      },
      fileList: [],
      uploadImgUrl: ""
    };
  },
  created() {
    this.uploadImgUrl = this.value;
  },
  methods: {
    getImages(el) {
      var file = el.target.files[0];
      var type = file.type.split("/")[0];
      if (type === "image") {
        this.upload(file);
      } else {
        this.$message.warn("只能上次图片格式");
      }
    },
    upload(imgUrl) {
      var that = this;
      var formdata = new FormData();
      formdata.append("file", imgUrl);
      axios
        .post(this.requestUrl, formdata, {
          headers: that.headers
        })
        .then(response => {
          let res = response.data;
          if (res.code == "200") {
            that.uploadImgUrl = res.data.urlPath;
            that.$emit("input", that.uploadImgUrl);
            that.$emit("change", that.uploadImgUrl);
          }
        });
    },
    changeInput(e) {
      if (e) {
        this.uploadImgUrl = e;
      } else {
        this.$refs.files.value = "";
        this.uploadImgUrl = "";
      }
      this.$emit("input", this.uploadImgUrl);
      this.$emit("change", this.uploadImgUrl);
    }
  }
};

// css

.file {
  position: absolute;
  width: 100%;
  padding: 100%;
  right: 0;
  top: 0;
  opacity: 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3 中实现文件上传可以通过使用 `<input type="file">` 元素和监听其 `change` 事件来实现。下面是一个简单的示例: ```vue <template> <div> <input type="file" @change="handleFileUpload"> <button @click="uploadFile">上传</button> </div> </template> <script> export default { data() { return { file: null }; }, methods: { handleFileUpload(event) { this.file = event.target.files[0]; }, uploadFile() { if (this.file) { // 创建一个 FormData 对象 const formData = new FormData(); formData.append('file', this.file); // 发送文件上传请求 // 这里可以使用 axios 或其他 HTTP 库发送请求 // 假设有一个名为 upload 的 API 接口用于文件上传 axios.post('/api/upload', formData) .then(response => { // 文件上传成功的处理逻辑 console.log('文件上传成功'); }) .catch(error => { // 文件上传失败的处理逻辑 console.error('文件上传失败', error); }); } } } }; </script> ``` 在上面的示例中,我们使用 `<input type="file">` 元素来让用户选择要上传的文件,并在 `@change` 事件中获取选中的文件对象,并赋值给 `file` 数据属性。 然后,当用户点击 "上传" 按钮时,我们将选中的文件对象添加到一个 FormData 对象中,并发送一个 POST 请求到服务器的 `/api/upload` 接口。你可以根据实际情况修改接口的 URL。 在服务器端,你需要处理文件上传的逻辑,并做相应的处理。这里我们使用了 axios 库来发送 HTTP 请求,你也可以选择其他的库或原生的 XMLHttpRequest 来发送请求。 请注意,在实际开发中,你还需要处理文件上传过程中的错误、进度等情况。上面的示例只是一个简单的演示,你可以根据自己的需求进行扩展和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值