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

相关文章

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

关于MongoDB图片URL存储异常问题以及解决

《关于MongoDB图片URL存储异常问题以及解决》:本文主要介绍关于MongoDB图片URL存储异常问题以及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录MongoDB图片URL存储异常问题项目场景问题描述原因分析解决方案预防措施js总结MongoDB图

python实现svg图片转换为png和gif

《python实现svg图片转换为png和gif》这篇文章主要为大家详细介绍了python如何实现将svg图片格式转换为png和gif,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录python实现svg图片转换为png和gifpython实现图片格式之间的相互转换延展:基于Py

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取

Python实现图片分割的多种方法总结

《Python实现图片分割的多种方法总结》图片分割是图像处理中的一个重要任务,它的目标是将图像划分为多个区域或者对象,本文为大家整理了一些常用的分割方法,大家可以根据需求自行选择... 目录1. 基于传统图像处理的分割方法(1) 使用固定阈值分割图片(2) 自适应阈值分割(3) 使用图像边缘检测分割(4)

springboot上传zip包并解压至服务器nginx目录方式

《springboot上传zip包并解压至服务器nginx目录方式》:本文主要介绍springboot上传zip包并解压至服务器nginx目录方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录springboot上传zip包并解压至服务器nginx目录1.首先需要引入zip相关jar包2.然

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例