vue两种上传文件(图片)方式

方式一:base64

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>文件上传</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      height: 100vh;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: #ddd;
    }

    .upload {
      width: 500px;
      height: 500px;
      padding: 10px;
      background-color: #fff;
      border-radius: 8px;

    }

    .upload span {
      font-size: 20px;
    }

    .u-input {
      position: relative;
      width: 100px;
      height: 40px;
    }

    .input {
      opacity: 0;
      cursor: pointer;
      position: absolute;
      z-index: 99;
      height: 100%;
      width: 100%;
    }

    .btn {
      width: 100px;
      height: 40px;
      background-color: #2985e9;
      border-radius: 8px;
      border: 0;
      color: #fff;
    }

    .btn2 {
      width: 60px;
      height: 30px;
      background-color: #fff;
      border-radius: 8px;
      border: 1px solid #ccc;

    }

    table {
      width: 480px;
      /* height: 400px; */
      border: 1px solid #ccc;
      border-collapse: collapse;
    }

    td,
    tr {
      border: 1px solid #ccc;
    }

    tr {
      height: 40px;
    }

    tr:nth-child(2n) {
      background-color: #ddd;
    }

    .footer {
      float: right;
    }

    .footer button {
      width: 60px;
      height: 40px;
      background-color: #2985e9;
      border-radius: 8px;
      border: 0;
      color: #fff;
    }
  </style>

</head>

<body>
  <div id="root">
    <div class="upload">
      <div><span>上传附件</span></div>
      <div class="u-input">
        <input ref="fileRef" class="input" type="file" @change="handleChange" />
        <button class="btn" @click="submitClick">选择文件</button>
      </div>
      <table>
        <thead>
          <tr style="display: none;"></tr>
          <tr>
            <td>名称</td>
            <td>大小</td>
            <td>操作</td>
          </tr>

        </thead>
        <tbody>
          <tr v-for="(item, index) in fileList" :key="index">
            <td>{{item.fileName}}</td>
            <td>{{item.fileSize}}kb</td>
            <td>
              <button class="btn2" @click="delFile(item)">删除</button>
            </td>
          </tr>
        </tbody>
      </table>
      <div class="footer">
        <button @click="cancel">取消</button>
        <button @click="upload">上传</button>
      </div>

    </div>
  </div>

</body>
<script src="./js/vue.js"></script>
<script src="./js/axios.js"></script>
<script>
  const vm = new Vue({
    el: '#root',
    data: {
      fileList: [],
      base64File: ""
    },
    methods: {
      submitClick () {
        console.log("submitClick")
      },
      handleChange (file, warp) {
        console.log(this.$refs["fileRef"].files[0])
        const uploadFile = this.$refs["fileRef"].files[0]
        const fileSize = uploadFile.size / 1024  //kb
        const fileName = uploadFile.name

        let fileObj = {
          fileName: fileName,
          fileSize: (fileSize).toFixed(1)
        }

        //转base64传输
        this.transferBase64(uploadFile, fileObj)



      },
      transferBase64 (files, obj) {
        // 先判断浏览器是否支持
        if (window.FileReader === 'undefined') {
          alert("您的浏览器不支持图片上传")
          return
        }
        // fileReader读取文件,可以转化为base64
        const reader = new FileReader()
        reader.readAsDataURL(files)  // 转为base64


        /*       
        FileReader.onabort:该事件是中止读取的时候触发。
        FileReader.onerror:该事件是读取发生错误的时候触发。
        FileReader.onload:该事件是读取完成的时候触发。
        FileReader.onloadstart:该事件是读取操作刚开始的时候触发。
        FileReader.onloadend:该事件是读取结束的时候触发(失败和成功的时候都会触发)。
        FileReader.onprogress:该事件在读取的时候触发。 
        */
        reader.onload = () => {
          this.fileList.push(obj)
          console.log(typeof reader.result) //string
          this.base64File = reader.result
          // axios.post("xxxx", {
          //   file: reader.result
          // }).then((res) => {

          // })
        }


      },
      delFile (item) {
        let arr = this.fileList.filter((v) => v.fileName !== item.fileName)
        this.fileList = arr

      },
      cancel () {
        this.fileList = []
      },
      upload () {
        axios.post("xxxxx", {
          file: this.base64File,
          params: this.fileList[0]
        }, {
          headers: {
            "Content-Type": "multipart/form-data"
          }
        }).then((res) => {

        })
      }
    },
  })
</script>

</html>

方式二: formData

<script>
  const vm = new Vue({
    el: '#root',
    data: {
      fileList: [],
      base64File: ""
    },
    methods: {
      submitClick () {
        console.log("submitClick")
      },
      handleChange (file, warp) {
        console.log(this.$refs["fileRef"].files[0])
        const uploadFile = this.$refs["fileRef"].files[0]
        const fileSize = uploadFile.size / 1024  //kb
        const fileName = uploadFile.name

        let fileObj = {
          fileName: fileName,
          fileSize: (fileSize).toFixed(1)
        }

        let formdata = new FormData()
        formdata.append('file', uploadFile)
        formdata.append('title', "abaaba")
        formdata.append('url', "d:/xxx")
        console.log(formdata.get("file"))
        /* axios.post('xxx', formdata, {
          headers: {
            "Content-Type": "multipart/form-data"
          }
        }) */



      },

      delFile (item) {
        let arr = this.fileList.filter((v) => v.fileName !== item.fileName)
        this.fileList = arr

      },
      cancel () {
        this.fileList = []
      },
      upload () {
        axios.post("xxxxx", {
          file: this.base64File,
          params: this.fileList[0]
        }).then((res) => {

        })
      }
    },
  })
</script>

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值