1.0.2 • Published 5 years ago

i-image-upload v1.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

i-image-upload vue图片上传组件

vue picture component. Resize and preview your uploaded image

使用指南

import iImageUpload from 'i-image-upload'
import 'i-image-upload/dist/i-image-upload.css'

Vue.use(iImageUpload)

i-image-upload 代码演示

普通表单上传

可以在组件内部添加子节点传入自定义的上传按钮类型和文字提示

<form 
  action="http://localhost:1400/avatar" 
  method="post" 
  enctype="multipart/form-data"
>
  <i-image-upload name="file">
    <i class="demo-icon">+</i>
  </i-image-upload>
  <button>上传</button>
</form>

默认上传

使用 before-upload 限制用户上传的图片格式和大小, 只有返回 true, 才会继续上传图片,否则停止

<template>
  <form>
    <i-image-upload 
      name="file" 
      action="http://localhost:1400/avatar" 
      :before-upload="beforeUpload"
      :on-preview="onPreview"
      :on-success="onSuccess"
      :on-error="onError"
    >
      <img v-if="imagePreview" :src="imagePreview" class="demo-avatar">
      <i class="demo-icon" v-else>+</i>
    </i-image-upload>
  </form>
</template>

<script>
export default {
  data () {
    return {
      imagePreview: ''
    }
  },
  methods: {
    beforeUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        console.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        console.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    onPreview (file) {
      this.imagePreview = file
    },
    onSuccess (res, dataurl) {
      console.log(res)
    },
    onError (res, dataurl) {
      console.log(res)
      this.imagePreview = ''
    }
  }
}
</script>

<style scoped>
.demo-icon {
  border: 1px dashed lightblue;
  width: 100px;
  height: 100px;
  text-align: center;
  line-height: 100px;
  display: block;
  font-style: normal;
  color: lightblue;
  font-size: 26px;
}
.demo-avatar {
  border: 1px dashed lightblue;
  width: 100px;
  height: 100px;
}
</style>

自定义ajax上传

使用 custom-handle-upload 覆盖默认的上传行为,可以自定义上传的实现

<template>
  <form>
    <i-image-upload 
      name="file" 
      action="http://localhost:1400/avatar" 
      resultType="blob"
      :before-upload="beforeUpload"
      :customHandleUpload="onhandleUpload"
      :on-success="onSuccess"
      :on-error="onError"
    >
      <img v-if="imagePreview" :src="imagePreview" class="demo-avatar">
      <i class="demo-icon" v-else>+</i>
    </i-image-upload>
  </form>
</template>

<script>
export default {
  data () {
    return {
      imagePreview: ''
    }
  },
  methods: {
    beforeUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        console.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        console.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    onhandleUpload (formatFile) {
      this.imagePreview = formatFile
      let filedata = new FormData()
      filedata.append('file', data)

      return fetch('http://localhost:1400/avatar', {
        method: 'post',
        mode: 'no-cors',
        body: filedata
      })
    },
    onSuccess (res, dataurl) {
      console.log(res)
      this.imagePreview = res.data.imageUrl
    },
    onError (res, dataurl) {
      console.log(res)
      this.imagePreview = ''
    }
  }
}
</script>

API

参数说明类型默认值
name上传的图片字段名Stringfile
disabled是否禁用Booleanfalse
maxWidth不压缩上传的最大宽度autoScale为true的时候才有用Number1024
maxHeight不压缩上传的最大高度autoScale为true的时候才有用Number1024
autoScale上传图片超出maxWidth或者maxHeight时自动压缩Booleantrue
quality上传图像的质量,0 - 1.00之间的浮点数Booleantrue
scaleRatio上传图像的缩放比例,接受0 - 1之间的浮点数, 譬如0.5指缩放为原来的一半Number-
autoRotate解析图像中的EXIF信息,并在上传之前正确旋转图像Booleantrue
capture设置可选的捕获属性String-
resultType返回上传图片或者预览图片所需要的格式String ['base64', 'blob']base64
action默认上传行为的上传地址String-
custom-handle-upload覆盖默认的上传行为,可以自定义上传的实现function(file, extra)
before-upload上传图片之前的钩子,参数为上传的图片,只有返回 true, 才会继续上传图片,否则停止function(file)
on-preview预览上传图片的钩子function(file)
on-progress图片默认ajax上传时的钩子function(event, dataurl)
on-success图片上传成功时的钩子function(response, dataurl)
on-error图片上传失败时的钩子function(response, dataurl)