首页 > 其他分享 >el-upload---上传/下载图片

el-upload---上传/下载图片

时间:2023-03-27 14:56:38浏览次数:34  
标签:el res upload fileList --- error message data condition

上传照片

          <el-upload
            ref="upload"
            :file-list="fileList"
            action="#"
            :auto-upload="false"
            :on-change="handleChange"
            :on-remove="handleRemove"
            :limit="2"
            list-type="picture-card"
          >
            <i
              class="el-icon-plus"
              :style="{ display: fileList.length ? 'none' : 'inline-block' }"
            ></i>
            <span
              :style="{ display: fileList.length ? 'inline-block' : 'none' }"
              >点击重新选择</span
            >
          </el-upload>


  data: () => {
    return {
      newData: {},
      fileList: [], //图片信息
    };
  },


// 上传照片
methods:{
async addData() { let condition = new FormData(); condition.append("fileId", this.info.fileId); condition.append("id", this.info.id); condition.append("comment", this.comment); condition.append("files", this.fileList[0]?.raw || await this.Base64ToFile(this.fileList[0]?.url, this.fileList[0]?.name)); uploadFile(condition) .then((res) => { if (res.data.code === 200) { this.$message.success("上传成功!"); this.$emit("successHandle") } else { this.$message.error(res.data.message); } }) .catch((error) => { this.$message.error({ message: failResponse(error).message, }); }); }, // 将base64转换为file Base64ToFile(dataurl, fileName) { const arr = dataurl.split(","), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]); let n = bstr.length, u8arr = new Uint8Array(n); while (n--) { u8arr[n] = bstr.charCodeAt(n); } return new File([u8arr], fileName, { type: mime }); },
      handleChange(files, fileList) {       if (fileList.length > 1) { //限制只可上传一张         fileList.splice(0, 1);       }       this.fileList = fileList;     },     handleRemove(files, fileList) {       this.fileList = [];     },

}

 查看照片

// 查看照片
    getData() {
      getFile({
        fileId: this.info.fileId,
      })
        .then((res) => {
          this.comment = res.data.data.comment;
          this.fileList = [{name: res.data.data.fileName, url: 'data:image/png;base64,'+this.ArrayBufferToBase64(res.data.data.files)}]
        })
        .catch((error) => {
          this.$message.error({
            message: failResponse(error).message,
          });
        });
    },

// byte数组转字符串
    ArrayBufferToBase64(buffer) {
      // 第一步:将arrayBuffer转为二进制字符串
      let binary = '';
      let bytes = new Uint8Array(buffer);
      for (let len = bytes.byteLength, i =0; i<len;i++) {
        binary += String.fromCharCode(bytes[i]);
      }
      // 将二进制字符串转为base64字符串
      return window.btoa(binary);
    },

 

标签:el,res,upload,fileList,---,error,message,data,condition
From: https://www.cnblogs.com/lftBlogs/p/17261529.html

相关文章

  • 数据结构与算法基础-----------树与二叉树
                         ......
  • 火山引擎 DataTester:构建增长闭环,3-5 人即可搭建企业增长团队
    更多技术交流、求职机会,欢迎关注字节跳动数据平台微信公众号,回复【1】进入官方交流群 增长是一个老生常谈的话题,对许多企业而言这个词都很熟悉,但在实践过程中仍然有很......
  • Android高版本---app网络错误
    此问题对应的安卓版本一般为Android9.0及以上现象:模拟器上运行时,网络无问题,接口通讯畅通;打包后运行,app显示网络错误排查:查看设备当前Android版本号,是否是9.0以及上......
  • spring-web中的annotation注解之十:@ModelAttribute
    spring-web中的annotation注解之十:@ModelAttribute1、@ModelAttribute注解在方法上:*该方法在Controller每个方法执行前会被调用,没有返回值的@ModelAttribute方法使用mo......
  • AcWing1024 -- 记忆化搜索 & 天梯赛
    1.题目描述2022年天梯赛正赛\(DIV2\)2.思路首先认真读题,题目说的是每次送完外卖之后不必返回起点。另外,需要送外卖的点是逐个添加,每添加一次都要算一次最短路......
  • vue组件化开发---插槽的使用
    插槽基本介绍在开发中,我们会经常封装一个个可复用的组件:前面我们会通过props传递给组件一些数据,让组件来进行展示;但是为了让这个组件具备更强的通用性,我们不能将组件中......
  • first-letter 伪元素
    first-letter伪元素"first-letter"伪元素用于向文本的首字母设置特殊样式:实例p:first-letter{color:#ff0000;font-size:xx-large;}注意: "first-letter"伪......
  • :first-line 伪元素
    :first-line伪元素"first-line"伪元素用于向文本的首行设置特殊样式。在下面的例子中,浏览器会根据"first-line"伪元素中的样式对p元素的第一行文本进行格式化:实......
  • python+playwright 学习-39.登录页面滑动解锁(ActionChains)
    前言登录页面会遇到滑块解锁,滑动解锁的目的就是为了防止别人用代码登录(也就是为了防止你自动化登录),有些滑动解锁是需要去拼图这种会难一点。有些直接拖到最最右侧就可以......
  • 万事达卡与德国电信旗下T-Mobile合作在欧洲推出NFC移动支付
    NFC钱包解决方案,不久之后再推广到德国。不过目前美国市场还不在考虑范围内。 德国电信在欧洲拥有9300万移动电话用户,在全世界范围内也拥有超过1.29亿用户。依靠德国电信......