Socket
Socket
Sign inDemoInstall

i-image-upload

Package Overview
Dependencies
12
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    i-image-upload

vue picture component. Resize and preview your uploaded image


Version published
Weekly downloads
1
decreased by-75%
Maintainers
1
Install size
4.27 MB
Created
Weekly downloads
 

Readme

Source

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)

Keywords

FAQs

Last updated on 13 Nov 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc