uniapp+uview封装上传图片组件(单张/多张)

2024-01-24 13:44

本文主要是介绍uniapp+uview封装上传图片组件(单张/多张),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

uniapp+uview封装上传图片组件(单张/多张)

先看效果
在这里插入图片描述

1.uploadImg.vue

<template><view class="uploadImg flex-d flex-wrap"><!-- 多张图片上传 --><view class="imgList imgArr flex-d flex-wrap" v-for="(item,index) in fileList" :key="index" v-if="maxCount>1 && fileList.length"><image class="imgListArr" :src="item" @click="clickImg(item)"></image><view class="iconClose" @click.stope="closeImgOne(index)"><u-icon name="close-circle-fill" color="rgba(234, 30, 55, 1)" size="24"></u-icon></view></view><!-- 单张图片上传 --><view class="imgList imgArr" v-if="oneImgurl && maxCount==1"><image :src="oneImgurl" @click="clickImg(oneImgurl)"></image><view class="iconClose" @click.stope="closeImg"><u-icon name="close-circle-fill" color="rgba(234, 30, 55, 1)" size="24"></u-icon></view></view><u-upload @afterRead="uploadImgFile" @delete="deletePic" name="3" :previewFullImage="true" :maxCount="maxCount" :width="width" :height="height" :multiple="multiple" maxSize="50242880" :deletable="deletable" v-if="!imgUrl && maxCount<2 && !multiple"></u-upload><u-upload @afterRead="uploadImgFile" @delete="deletePic" name="3" :previewFullImage="true" :maxCount="maxCount" :width="width" :height="height" :multiple="multiple" maxSize="50242880" :deletable="deletable" v-if="multiple && (fileList.length<maxCount)"></u-upload></view>
</template><script>const app = getApp()export default {props: {maxCount: { //多张图片上传个数type: String,default: '1'},width: { //多张图片的宽度type: Number | String,default: 104},height: { //多张图片的高度type: Number | String,default: 104},file: { //多张图片返回的数组type: Array,value: []},imgUrl: { //单张图片返回的地址type: String,default: ''},multiple: { // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式type: Boolean,default: false},scene: { //参数type: String,default: ''},field: { //返回的字段名称type: String,default: ''}},watch: {file: {handler(oldValue) {this.fileList = oldValue}},imgUrl: {handler(newval) {this.oneImgurl = newval}}},data() {return {fileList: this.file,show: false,img: {},oneImgurl: this.imgUrl,deletable: false,clipperWidth: app.globalData.screenWidth}},methods: {// 删除图片deletePic(event) {this.fileList.splice(event.index, 1)this.$emit('getImgList', this.fileList)},// 上传图片uploadImgFile(event) {this.afterRead(event)},// 新增图片async afterRead(event) {// 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式let lists = event.fileif (this.maxCount < 2) {const result = await this.uploadFilePromise(event.file)this.$emit('getImg', {url: result.data.original,field: this.field})} else {for (let i = 0; i < lists.length; i++) {const result = await this.uploadFilePromise(lists[i])this.fileList.push(result.data.original)this.$emit('getImg', {list: this.fileList,field: this.field})}}},uploadFilePromise(item) {var formData = {};return new Promise((resolve, reject) => {formData.scene = this.scenelet a = uni.uploadFile({url: app.globalData.reqUrl + '/****/****/***',filePath: item.url,name: 'image',formData: formData,success: (res) => {let datas = JSON.parse(res.data)resolve(datas)},fail: (err) => {uni.showToast({title: '上传失败',icon: 'none'})}});})},// 数组图片删除单个closeImgOne(idx) {this.fileList.splice(idx, 1)this.$emit('getImg', {list: this.fileList,field: this.field})},// 单个图片上传closeImg() {this.oneImgurl = ''this.$emit('getImg', {url: this.oneImgurl,field: this.field})},// 图片放大查看clickImg(url) {let _this = this;let imgsArray = [];imgsArray[0] = urluni.previewImage({current: 0,urls: imgsArray});}}}
</script><style lang="scss" scope>.imgList {image {width: 168rpx;height: 168rpx;border-radius: 16rpx;margin: 0 28rpx 20rpx 0}}.imgListArr {width: 168rpx;height: 168rpx;border-radius: 16rpx;margin: 0 30rpx 30rpx 0}.imgArr {position: relative;}.iconClose {position: absolute;top: -20rpx;right: -10rpx;}
</style>

使用方式test.vue

// 多张
<uploadImg :file="imgList" maxCount='5' field="imgListItem" @getImg="getImg" :multiple="true" width="168rpx" height="168rpx" scene="err_image" need_thumbnail="1">
</uploadImg>// 单张
<!-- <uploadImg :imgUrl="img" maxCount='1' @getImg="getImg" field="img">
</uploadImg> -->// js// 获取图片
getImg(val) {console.log(val)
},

这篇关于uniapp+uview封装上传图片组件(单张/多张)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/639876

相关文章

Python实现精准提取 PDF中的文本,表格与图片

《Python实现精准提取PDF中的文本,表格与图片》在实际的系统开发中,处理PDF文件不仅限于读取整页文本,还有提取文档中的表格数据,图片或特定区域的内容,下面我们来看看如何使用Python实... 目录安装 python 库提取 PDF 文本内容:获取整页文本与指定区域内容获取页面上的所有文本内容获取

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.

Go语言如何判断两张图片的相似度

《Go语言如何判断两张图片的相似度》这篇文章主要为大家详细介绍了Go语言如何中实现判断两张图片的相似度的两种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 在介绍技术细节前,我们先来看看图片对比在哪些场景下可以用得到:图片去重:自动删除重复图片,为存储空间"瘦身"。想象你是一个

使用Python实现base64字符串与图片互转的详细步骤

《使用Python实现base64字符串与图片互转的详细步骤》要将一个Base64编码的字符串转换为图片文件并保存下来,可以使用Python的base64模块来实现,这一过程包括解码Base64字符串... 目录1. 图片编码为 Base64 字符串2. Base64 字符串解码为图片文件3. 示例使用注意

Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析

《Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析》InstantiationAwareBeanPostProcessor是Spring... 目录一、什么是InstantiationAwareBeanPostProcessor?二、核心方法解

c/c++的opencv实现图片膨胀

《c/c++的opencv实现图片膨胀》图像膨胀是形态学操作,通过结构元素扩张亮区填充孔洞、连接断开部分、加粗物体,OpenCV的cv::dilate函数实现该操作,本文就来介绍一下opencv图片... 目录什么是图像膨胀?结构元素 (KerChina编程nel)OpenCV 中的 cv::dilate() 函

C++ RabbitMq消息队列组件详解

《C++RabbitMq消息队列组件详解》:本文主要介绍C++RabbitMq消息队列组件的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. RabbitMq介绍2. 安装RabbitMQ3. 安装 RabbitMQ 的 C++客户端库4. A

GitLab文件的上传与下载方式

《GitLab文件的上传与下载方式》:本文主要介绍GitLab文件的上传与下载方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录GitLab 项目拉取到本地GitLab 项目上传方法方法 1:本地项目未初始化Git方法 2:本地项目已初始化GitGitLab 上

使用Python实现调用API获取图片存储到本地的方法

《使用Python实现调用API获取图片存储到本地的方法》开发一个自动化工具,用于从JSON数据源中提取图像ID,通过调用指定API获取未经压缩的原始图像文件,并确保下载结果与Postman等工具直接... 目录使用python实现调用API获取图片存储到本地1、项目概述2、核心功能3、环境准备4、代码实现

Nginx 413修改上传文件大小限制的方法详解

《Nginx413修改上传文件大小限制的方法详解》在使用Nginx作为Web服务器时,有时会遇到客户端尝试上传大文件时返回​​413RequestEntityTooLarge​​... 目录1. 理解 ​​413 Request Entity Too Large​​ 错误2. 修改 Nginx 配置2.1