vue 图片上传

功能说明:

1、调用手机拍照功能

2、调用相册功能

3、图片上传功能

4、图片预览功能

5、图片删除功能

记录个多图上传组件,关键点:
1、input 新增multiple
2、accept="image/*处理IOS、安卓手机启用摄像头
3、根据createObjectURL处理图片预览
4、formData构造函数创建空对象
5、通过formData.getAll(),把文件流数据转为数组

组件示例图

组件代码 upload_img.vue

<template>
  <div class="content">
    <div class="imgBox">
      <div class="uploadBox" v-for="(value, key) in imgs" :key="‘uploadBox‘ + key">
        <img :src="getObjectURL(value)" alt="" class="uploadImg">
        <img src="../../assets/images/icon_x.png" alt="" class="closeImg" @click=‘delImg(key)‘>
      </div>
      <div class="inputButton">
      <img src="../../assets/images/icon_photo.png" alt="" class="addImg">
        <input type="file" class="upload" @change="addImg" ref="inputer" multiple
          accept="image/*"/>
      </div>
      <div class="submitTask" @click="submit">
        上传图片
      </div>
    </div>
  </div>
</template>

js:

export default ({
  name: ‘uploadPicture‘,
  data: function() {
    return {
      formData: new FormData(),
      imgs: {},
      imgLen: 0,
      txtVal: 0,
      desc: ‘‘

    }
  },
  created() {

  },
  methods: {
    descInput() {
      // this.txtVal = this.desc.length;
    },
    addImg(event) {
      let inputDOM = this.$refs.inputer
      // 通过DOM取文件数据
      this.fil = inputDOM.files
      // console.log(inputDOM.files)
      let oldLen = this.imgLen
      for (let i = 0; i < this.fil.length; i++) {
        let size = Math.floor(this.fil[i].size / 1024)
        if (size > 5 * 1024 * 1024) {
          alert(‘请选择5M以内的图片!‘)
          return false
        }
        this.imgLen++
        this.$set(this.imgs, this.fil[i].name + ‘?‘ + new Date().getTime() + i, this.fil[i])
        // console.log(this.imgs)
      }
      console.log(‘this.fil‘, this.fil)
      // this.imgs = this.fil
    },
    getObjectURL(file) {
      var url = null
      if (window.createObjectURL !== undefined) { // basic
        url = window.createObjectURL(file)
      } else if (window.URL !== undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file)
      } else if (window.webkitURL !== undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file)
      }
      return url
    },
    delImg(key) {
      this.$delete(this.imgs, key)
      this.imgLen--
    },
    // 提交上传图片
    submit() {
      for (let key in this.imgs) {
        let name = key.split(‘?‘)[0]
        this.formData.append(‘file‘, this.imgs[key])
      }
      this.formData.getAll(‘file‘)
      console.log(‘this.formData‘, this.formData)
      this.$refund.upload(this.formData)
    }
  }
})
</script>

说明:

图片上传完成后,使用getObjectURL来作预览功能处理。

温馨提示:

formData.getAll最后返回来的是一个数组

样式:

<style lang="less">
// 外部盒子样式
.content {
  width: 100%;
  padding: 0.16rem 0 !important;
  .imgBox {
    display: flex;
    display: -webkit-flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;
    flex-wrap: wrap;
  }
}

// 预览图片样式
.uploadBox {
  position: relative;
  width: 30%;
  text-align: left;
  margin-right: 0.1rem;
  margin-bottom: 0.05rem;
  // 预览样式
  .uploadImg {
    width: 100%;
    height: 100%;
    display: block;
    overflow: hidden;
  }
  // 删除按钮
  .closeImg {
    width: 0.2rem;
    height: 0.2rem;
    position: absolute;
    top: 2%;
    right: 1%;
  }
}
// 上图图片icon
.inputButton {
  position: relative;
  display: block;
  width: 1.2rem;
  height: 1.2rem;
  // 上传图片样式
  .addImg {
    display: block;
    width: 1.2rem;
    height: 1.2rem;
  }
  // input样式
  .upload {
    width: 1.2rem;
    height: 1.2rem;
    opacity: 0;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 100;
  }
}
// 提交按钮
.submitTask {
    margin: auto;
    background: #EF504D;
    width: 30%;
    height: 0.3rem;
    color: #fff;
    font-size: 0.16rem;
    line-height: 0.3rem;
    text-align: center;
    border-radius: 0.1rem;
    margin-top: 0.8rem;
}
</style>

欢迎关注公众号,进一步技术交流:

原文地址:https://www.cnblogs.com/cczlovexw/p/11833930.html

时间: 2024-08-29 03:43:39

vue 图片上传的相关文章

vue 图片上传功能

这次做了vue页面的图片上传功能,不带裁剪功能的! 首先是html代码,在input框上添加change事件,如下: <ul class="clearfix">   <li v-if="imgs.length>0" v-for='(item ,index ) in imgs'>   <img :src="item">   </li>   <li style="position:

Vue 做图片上传

Vue图片单图或者多图上传代码如下: <template> <div class="upimgBox"> <div class="container"> <div class="upload_warp_left" @click="fileClick" > <div class="addNote">+</div> </div>

vue项目富文本编辑器vue-quill-editor之自定义图片上传

使用富文本编辑器的第一步肯定是先安装依赖 npm i vue-quill-editor 1.如果按照官网富文本编辑器中的图片上传是将图片转为base64格式的,如果需要上传图片到自己的服务器,需要修改配置. 创建一个quill-config.js的文件,里面写自定义图片上传.代码如下 /*富文本编辑图片上传配置*/ const uploadConfig = { action: '', // 必填参数 图片上传地址 methods: 'POST', // 必填参数 图片上传方式 token: ''

vue使用富文本编辑器vue-quill-editor实现配合后台将图片上传至七牛

一.全局注册:main.js import Vue from 'vue' import VueQuillEditor, { Quill } from 'vue-quill-editor' import { ImageDrop } from 'quill-image-drop-module' import ImageResize from 'quill-image-resize-module' import 'quill/dist/quill.core.css' import 'quill/dis

vue+element ui +vue-quill-editor 富文本图片上传到骑牛云

vue-quill-editor上传图片会转换成base64格式,但是这不是我们想要的,之后翻了很多文章才找到想要的,下面直接上代码 <style lang="sass">.quill-editor min-height: 500px background:#fff .ql-container min-height: 500px .ql-snow .ql-editor img max-width: 480px .ql-editor .ql-video max-width:

vue富文本编辑,编辑自动预览,单个图片上传不能预览的问题解决:

//预览<div class="htmlViewBox"> <p v-html="activity_html_defaultMsg" v-show="htmlDefaultMsg"></p> <p v-show="defaultMsg=='' && htmlDefaultMsg==''">请在富文本编辑器内容</p></div>//编辑器&

vue 的图片上传,压缩,exif图片信息识别(手机拍摄横板问题)

1.file表单组件监听change获取file文件 2.创建fileReader 获取base64位图片路由 3.实例一个Image 将base64位图片路由对其进行赋值,创建canvas 通过drawImage方法对其进行等比例缩放复制,实现图片压缩 4.引入 exif-js 模块 获取旋转角度信息,非1的,对canvas进行旋转,重绘,再通过toDataURL 获取旋转矫正后的图片64位 5.调用回调函数进行图片上传 //upload import EXIF from "exif-js&q

VUE2.0+VUE-Router做一个图片上传预览的组件

之前发了一篇关于自己看待前端组件化的文章,但是由于学习和实践的业务逻辑差异,所以自己练习的一些demo逻辑比较简单,打算用vue重构现在公司做的项目,所以在一些小的功能页面上使用vue来做的,现在写的这个是项目中用户反馈功能而来的,收获还是挺多的. 收获:dom操作=>数据操作       router的使用       组件的使用,具体总结放在尾部. 功能:1.上传图片 2.显示缩略图 3.可以删除 4.可以重新选择文件 先上成品图(主要抽取图片这块),自己在家主要做的功能,样式就不计较了.

vuejs开发组件分享之H5图片上传、压缩及拍照旋转的问题处理

一.前言 三年.net开发转前端已经四个月了,前端主要用webpack+vue,由于后端转过来的,前端不够系统,希望分享下开发心得与园友一起学习. 图片的上传之前都是用的插件(ajaxupload),或者传统上传图片的方式,各有利弊:插件的问题是依赖jq并且会使系统比较臃肿,还有传统的web开发模式 前后端偶尔在一起及对用户体验要求低,现在公司采用webpack+vue+restfullApi开发模式 前后端完全分离,遵从高内聚,低偶尔的原则,开发人员各司其职,一则提升开发效率(从长期来看,短期