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

相关文章

input的accept属性让文件上传安全高效

《input的accept属性让文件上传安全高效》文章介绍了HTML的input文件上传`accept`属性在文件上传校验中的重要性和优势,通过使用`accept`属性,可以减少前端JavaScrip... 目录前言那个悄悄毁掉你上传体验的“常见写法”改变一切的 html 小特性:accept真正的魔法:让

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

JavaWeb 中的 Filter组件详解

《JavaWeb中的Filter组件详解》本文详细介绍了JavaWeb中的Filter组件,包括其基本概念、工作原理、核心接口和类、配置方式以及常见应用示例,Filter可以实现请求预处理、响应后... 目录JavaWeb 中的 Filter 详解1. Filter 基本概念1.1 什么是 Filter1.

2025最新版Android Studio安装及组件配置教程(SDK、JDK、Gradle)

《2025最新版AndroidStudio安装及组件配置教程(SDK、JDK、Gradle)》:本文主要介绍2025最新版AndroidStudio安装及组件配置(SDK、JDK、Gradle... 目录原生 android 简介Android Studio必备组件一、Android Studio安装二、A

Python多任务爬虫实现爬取图片和GDP数据

《Python多任务爬虫实现爬取图片和GDP数据》本文主要介绍了基于FastAPI开发Web站点的方法,包括搭建Web服务器、处理图片资源、实现多任务爬虫和数据可视化,同时,还简要介绍了Python爬... 目录一. 基于FastAPI之Web站点开发1. 基于FastAPI搭建Web服务器2. Web服务

前端Visual Studio Code安装配置教程之下载、汉化、常用组件及基本操作

《前端VisualStudioCode安装配置教程之下载、汉化、常用组件及基本操作》VisualStudioCode是微软推出的一个强大的代码编辑器,功能强大,操作简单便捷,还有着良好的用户界面,... 目录一、Visual Studio Code下载二、汉化三、常用组件1、Auto Rename Tag2

java对接Pinata上传文件到IPFS全过程

《java对接Pinata上传文件到IPFS全过程》本文详细介绍了如何使用PinataAPI将文件上传到IPFS网络,首先登录Pinata官网并生成JWT令牌,然后在项目中导入OkHttp依赖并编写代... 目录1.登录2.生成令牌3.导入依赖4.编写代码5.调用接口调试China编程代码总结Pinata调用AP

利用Python将PDF文件转换为PNG图片的代码示例

《利用Python将PDF文件转换为PNG图片的代码示例》在日常工作和开发中,我们经常需要处理各种文档格式,PDF作为一种通用且跨平台的文档格式,被广泛应用于合同、报告、电子书等场景,然而,有时我们需... 目录引言为什么选择 python 进行 PDF 转 PNG?Spire.PDF for Python

Vue3视频播放组件 vue3-video-play使用方式

《Vue3视频播放组件vue3-video-play使用方式》vue3-video-play是Vue3的视频播放组件,基于原生video标签开发,支持MP4和HLS流,提供全局/局部引入方式,可监听... 目录一、安装二、全局引入三、局部引入四、基本使用五、事件监听六、播放 HLS 流七、更多功能总结在 v

uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)

《uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)》在uni-app开发中,文件上传和图片处理是很常见的需求,但也经常会遇到各种问题,下面:本文主要介绍uni-app小程序项目中实... 目录方式一:使用<canvas>实现图片压缩(推荐,兼容性好)示例代码(小程序平台):方式二:使用uni