vue2.0 自定义 图片上传( UpLoader )组件

1.自定义组件

UpLoader.vue

<!-- 上传图片 组件 -->
<template>
  <div class="vue-uploader">
    <!-- 添加图片 及 显示效果 -->
    <div class="file-list">
      <!-- 图片列表 files -->
      <section v-for="(file, index) of files" class="file-item draggable-item">
        <img :src="file.src"  ondragstart="return false;">
        <p class="file-name">{{file.name}}</p>
        <span class="file-remove" @click="remove(index)">+</span>
      </section>
      <!-- 添加图片按钮 -->
      <section v-if="status == ‘ready‘" class="file-item">
        <div @click="add" class="add">
          <span>+</span>
        </div>
      </section>
    </div>
    <!-- 上传图片操作 及 显示进程 -->
    <section v-if="files.length != 0" class="upload-func">
      <!-- 上传进度 -->
      <div class="progress-bar">
        <section v-if="uploading" :width="(percent * 100) + ‘%‘">{{(percent * 100) + ‘%‘}}</section>
      </div>
      <!-- 操作按钮 -->
      <div class="operation-box">
        <button v-if="status == ‘ready‘" @click="submit">上传</button>
        <button v-if="status == ‘finished‘" @click="finished">完成</button>
      </div>
    </section>
    <!-- 调用相机/图库 ref="file" 指定DOM节点 -->
    <input type="file" accept="image/*" @change="fileChanged" ref="file" multiple="multiple">
  </div>
</template>

<script>
  export default {
    props: {
      src: { // 后台接受图片的http地址
        type: String,
        required: true
      }
    },
    data() {
      return {
        status: ‘ready‘, // 状态
        files: [], // 图片数组
        point: {},
        uploading: false, // 进度条
        percent: 0, // 上传进度
      }
    },
    methods: {
      // 添加图片操作
      add() {
        this.$refs.file.click();
      },
      // 上传图片操作
      submit() {
        if (this.files.length === 0) {
          console.warn(‘no file!‘);
          return
        }
        // 创建formData对象
        const formData = new FormData();
        this.files.forEach((item) => {
          formData.append(item.name, item.file)
        })
        const xhr = new XMLHttpRequest()
        xhr.upload.addEventListener(‘progress‘, this.uploadProgress, false)
        xhr.open(‘POST‘, this.src, true)
        this.uploading = true
        xhr.send(formData)
        xhr.onload = () => {
          this.uploading = false
          if (xhr.status === 200 || xhr.status === 304) {
            this.status = ‘finished‘
            console.log(‘upload success!‘)
          } else {
            console.log(`error:error code ${xhr.status}`)
          }
        }
      },
      // 完成操作 还原状态
      finished() {
        this.files = []
        this.status = ‘ready‘
      },
      // 上传图片列表中的某个图片
      remove(index) {
        this.files.splice(index, 1)
      },
      // 唤醒相机/图库
      fileChanged() {
        const list = this.$refs.file.files
        for (let i = 0; i < list.length; i++) {
          if (!this.isContain(list[i])) {
            const item = {
              name: list[i].name,
              size: list[i].size,
              file: list[i]
            }
            // 转换图片格式
            this.html5Reader(list[i], item)
            this.files.push(item)
          }
        }
        this.$refs.file.value = ‘‘
      },
      // 将图片文件转成BASE64格式
      html5Reader(file, item){
        const reader = new FileReader()
        reader.onload = (e) => {
          this.$set(item, ‘src‘, e.target.result)
        }
        reader.readAsDataURL(file)
      },
      // 判断是否包含
      isContain(file) {
        this.files.forEach((item) => {
          if(item.name === file.name && item.size === file.size) {
            return true
          }
        })
        return false
      },
      // 上传进度
      uploadProgress(evt) {
        const component = this
        if (evt.lengthComputable) {
          const percentComplete = Math.round((evt.loaded * 100) / evt.total)
          component.percent = percentComplete / 100
        } else {
          console.warn(‘upload progress unable to compute‘)
        }
      }
    }
  }
</script>

<style lang="less" scoped>
  .vue-uploader {
    border: 1px solid #e5e5e5;
    .file-list {
      padding: 10px 0px;
      &:after {
        content: ‘‘;
        display: block;
        clear: both;
        visibility: hidden;
        line-height: 0;
        height: 0;
        font-size: 0;
      }
      .file-item {
        float: left;
        position: relative;
        width: 100px;
        text-align: center;
        img{
          width: 80px;
          height: 80px;
          border: 1px solid #ececec;
        }
        .file-remove {
          position: absolute;
          right: 12px;
          display: none;
          top: 4px;
          width: 14px;
          height: 14px;
          color: white;
          cursor: pointer;
          line-height: 12px;
          border-radius: 100%;
          transform: rotate(45deg);
          background: rgba(0, 0, 0, 0.5);
        }
        &:hover{
          .file-remove {
            display: inline;
          }
        }
        .file-name {
          margin: 0;
          height: 40px;
          word-break: break-all;
          font-size: 14px;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
        }
      }
    }
    .add {
      width: 80px;
      height: 80px;
      margin-left: 10px;
      float: left;
      text-align: center;
      line-height: 80px;
      border: 1px dashed #ececec;
      font-size: 30px;
      cursor: pointer;
    }
    .upload-func {
      display: flex;
      padding: 10px;
      margin: 0px;
      background: #f8f8f8;
      border-top: 1px solid #ececec;
      .progress-bar {
        flex-grow: 1;
        section {
          margin-top: 5px;
          background: #00b4aa;
          border-radius: 3px;
          text-align: center;
          color: #fff;
          font-size: 12px;
          transition: all .5s ease;
        }
      }
      .operation-box {
        flex-grow: 0;
        padding-left: 10px;
        button {
          padding: 4px 12px;
          color: #fff;
          background: #007ACC;
          border: none;
          border-radius: 2px;
          cursor: pointer;
        }
      }
    }
    & > input[type="file"] {
      display: none;
    }
  }
</style>

2.页面调用

UpLoadImg.vue

<!-- 上传图片 -->
<template>
  <div>
    <!-- 标题栏 -->
    <mt-header title="上传图片">
      <router-link to="/" slot="left">
        <mt-button icon="back">返回</mt-button>
      </router-link>
    </mt-header>
    <!-- 内容 -->
    <div style="width: 100%;">
      <m-up-loader :src="src"></m-up-loader>
    </div>
  </div>
</template>

<script>
  // 上传图片组件
  import mUpLoader from ‘../components/UpLoader‘

  export default {
    name: ‘UploadImg‘,
    components: {
      mUpLoader
    },
    data () {
      return {
        src: ‘/api/imgs‘, // 后台接受图片的路径
      }
    },
    mounted () {
      //
    },
    methods: {
      //
    }
  }
</script>

<style lang="less" scoped>
  .show {
    width: 100px;
    height: 100px;
    overflow: hidden;
    position: relative;
    border-radius: 50%;
    border: 1px solid #d5d5d5;
  }
  .picture {
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-position: center center;
    background-repeat: no-repeat;
    background-size: cover;
  }
</style>

3.效果图

.

时间: 2024-08-26 06:39:36

vue2.0 自定义 图片上传( UpLoader )组件的相关文章

ueditor 百度编辑器 自定义图片上传路径和格式化上传文件名

今天项目中需要自定义图片上传的保存路径,并且不需要按照日期自动创建存储文件夹和文件名,我的ueditor版本是1.3.6.下面记录一下我配置成功的方法,如果有什么不对的地方欢迎指出,共同学习: 1:我在本地项目的根目录下新建一个文件夹,比如:upload. 2:打开ueditor/php/config.php,改成如下代码后保存: <?php return array( //图片上传允许的存储目录 /*'imageSavePath' => array ( 'upload1', 'upload2

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

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

Jquery自定义图片上传插件

1 概述 编写后台网站程序大多数用到文件上传,可是传统的文件上传控件不是外观不够优雅,就是性能不太好看,翻阅众多文件上传控件的文章,发现可以这样去定义一个文件上传控件,实现的文件上传的效果图如下: 2.该图片上传插件实现功能如下: 1>能够异步上传,上传成功之后,服务器返回响应结果:能够定义上传前和上传后自定义处理方式: 2>能够实现文件格式判断,过滤非图片文件: 3>服务端能够过滤重复上传的图片: 3.页面代码分析: 1>.Jquery图片上传插件代码如下: // 选中文件, 提

rails使用bootstrap3-wysiwyg可视化编辑器并实现自定义图片上传插入功能

之前在rails开发中使用了ckeditor作为可视化编辑器,不过感觉ckeditor过于庞大,有很多不需要的功能,而且图片上传功能不好控制不同用户可以互相删除图片,感觉很不好.于是考虑更改可视化编辑器,多方考虑选择了bootstrap3-wysiwyg,但是这个编辑器无法实现图片上传功能,还有换行使用br而不是p标签不是很好.于是考虑自定义完善其功能. 个人原创,版权所有,转载请注明原文出处,并保留原文链接: https://www.embbnux.com/2015/03/17/rails_u

使用百度UMeditor富文本编辑器,修改自定义图片上传,修改源码

富文本编辑器,不多说了,这个大家应该都用到过,至于用到的什么版本,那就分很多种 CKEditor:很早以前叫FCK,那个时候也用过,现在改名了,比较流行的一个插件,国外很多公司在用 UEDITOR:百度开发的插件,lite版是UM EasyUI编辑器:用easyUI的都懂,基本上肯定用到 其他的富文本编辑器就不说了,前两个小编我用的比较多 本来我是比较倾向于CKEditor,但是这个插件不支持图片上传,图片功能只能链接过去,而不能在控件本身中上传,所以就选择了UMeditor 为啥选择UM,不选

html+css实现自定义图片上传按钮 &#207061;

原文: http://blog.gqylpy.com/gqy/492 置顶:来自一名75后老程序员的武林秘籍--必读(博主推荐) 来,先呈上武林秘籍链接:http://blog.gqylpy.com/gqy/401/ 你好,我是一名极客!一个 75 后的老工程师! 我将花两分钟,表述清楚我让你读这段文字的目的! 如果你看过武侠小说,你可以把这个经历理解为,你失足落入一个山洞遇到了一位垂暮的老者!而这位老者打算传你一套武功秘籍! 没错,我就是这个老者! 干研发 20 多年了!我也年轻过,奋斗过!我

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

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

Django(十九)文件上传:图片上传(后台上传、自定义上传)、

一.基本设置 参考:https://docs.djangoproject.com/zh-hans/3.0/topics/http/file-uploads/ 1)配置project1/settings.py 因为图片也属于静态文件,所以保存到static目录下. MEDIA_ROOT=os.path.join(BASE_DIR,"static/media") 2)在static目录下创建media目录,再创建应用名称的目录,此例为app1 F:\Test\django-demo\pro

yii2.0 图片上传(摘录)

文章来源:http://blog.sina.com.cn/s/blog_88a65c1b0101izmn.html 下面小伙就带领大学学习一下 Yii2.0 的图片上传类的使用,还是老样子,如果代码样式混乱,我会附上截图供大家学习. 1.UserController.php 很重要的一步,那就是 use yii\web\UploadedFile; public function actionUpload(){ $model = new User(); user 为用户表model: if ($m