位置: IT常识 - 正文

uniapp、uview——图片上传(单图上传、多图上传、多组照片上传、图片回显)(uniapp+uview)

发布时间:2024-01-03
uniapp、uview——图片上传(单图上传、多图上传、多组照片上传、图片回显) 一、简介

推荐整理分享uniapp、uview——图片上传(单图上传、多图上传、多组照片上传、图片回显)(uniapp+uview),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:uni-app app.vue,uniapp和uview,uniapp+uview,uniapp使用weui,uniapp v-for,uniapp使用weui,uniapp v-for,uniapp+uview,内容如对您有帮助,希望把文章链接给更多的朋友!

uView组件的上传功能,单图上传、多图上传等。 官方文档地址: https://www.uviewui.com/components/upload.html

二、步骤(一)单图上传1.效果演示:

只能上传一张,选完之后,上传的按钮消失,当然,如果图片不合适,删掉再换一张,但就是只能上传一张。

2.代码:<template><view class="content"><!-- 上传图片 --><u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" :multiple="false" :maxCount="1" width="112rpx" height="109rpx" :deletable="true" :previewImage="true"> <!-- 这张图片就是自定义的图片,地址填写自己本地的就行 --><image src="/static/function/uploadImg.png" mode="widthFix" style="width: 112rpx;height: 110rpx;"></image></u-upload></view></template><script>export default {data() {return {// 上传图片fileList1: [],}},onLoad() {},methods: { //删除图片deletePic(e) {console.log(e);this[`fileList${e.name}`].splice(e.index, 1)},// 新增图片async afterRead(event) {console.log(event)// 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式let lists = [].concat(event.file)let fileListLen = this[`fileList${event.name}`].lengthlists.map((item) => {this[`fileList${event.name}`].push({...item,status: 'uploading',message: '上传中'})})for (let i = 0; i < lists.length; i++) {const result = await this.uploadFilePromise(lists[i].url)let item = this[`fileList${event.name}`][fileListLen]this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {status: 'success',message: '',url: result}))fileListLen++}},//上传图片uploadFilePromise(url) {return new Promise((resolve, reject) => {let a = uni.uploadFile({//url: this.$common.domain+'/api/common/upload', // 仅为示例,非真实的接口地址url:'http://192.168.2.21:7001/upload', // 仅为示例,非真实的接口地址filePath: url,name: 'file',formData: {user: 'test'},success: (res) => {let data=JSON.parse(res.data) //最终传给的是字符串,这里需要转换格式resolve(data.data.url)}});})},}}</script><style lang="scss"></style>(二)多图上传1.效果演示:uniapp、uview——图片上传(单图上传、多图上传、多组照片上传、图片回显)(uniapp+uview)

可一次性选多张,我这里限制为两张,上传满两张则不会显示上传的logo。点击图片可预览。

2.代码:<template><view class="content"><!-- 上传图片 --><u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" :multiple="true" :maxCount="2" width="112rpx" height="109rpx" :deletable="true" :previewImage="true"> <!-- 这张图片就是自定义的图片,地址填写自己本地的就行 --><image src="/static/function/uploadImg.png" mode="widthFix" style="width: 112rpx;height: 110rpx;"></image></u-upload></view></template><script>export default {data() {return {// 上传图片fileList1: [],}},onLoad() {},methods: { //删除图片deletePic(e) {console.log(e);this[`fileList${e.name}`].splice(e.index, 1)},// 新增图片async afterRead(event) {console.log(event)// 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式let lists = [].concat(event.file)let fileListLen = this[`fileList${event.name}`].lengthlists.map((item) => {this[`fileList${event.name}`].push({...item,status: 'uploading',message: '上传中'})})for (let i = 0; i < lists.length; i++) {const result = await this.uploadFilePromise(lists[i].url)let item = this[`fileList${event.name}`][fileListLen]this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {status: 'success',message: '',url: result}))fileListLen++}},//上传图片uploadFilePromise(url) {return new Promise((resolve, reject) => {let a = uni.uploadFile({//url: this.$common.domain+'/api/common/upload', // 仅为示例,非真实的接口地址url:'http://192.168.2.21:7001/upload', // 仅为示例,非真实的接口地址filePath: url,name: 'file',formData: {user: 'test'},success: (res) => {setTimeout(() => {resolve(res.data.data)}, 1000)}});})},}}</script><style lang="scss"></style>三、其余补充

如果一个页面上有多处上传,操作也不是很复杂,大家都是共用同一个方法。 整体搬过来用即可。 请求接口的地方需要用join处理一下

getData() {let images=[]this.fileList1.forEach((item)=>{images.push(item.url)})this.$common.request('post', '/Coupon/addCoupon', {image:images.join(','),}).then(res => {if (res.code == 1) {this.$common.success(res.msg)setTimeout(()=>{this.$common.back()},1200)}})},新上传代码:(多图处理)<view class="imgBox"><u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" :multiple="true" :maxCount="9"><image :src="$common.image('/static/talentZone/addImg.png')" mode="aspectFill" class="fileImg"></image></u-upload></view>data() {return {// 图片列表fileList: []}},methods: {// 图片上传//删除图片deletePic(e) {console.log(e);this.fileList.splice(e.index, 1)},// 新增图片async afterRead(event) {// 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式let lists = [].concat(event.file)let fileListLen = this.fileList.lengthlists.map((item) => {this.fileList.push({...item,status: 'uploading',message: '上传中'})})for (let i = 0; i < lists.length; i++) {const result = await this.uploadFilePromise(lists[i].url)console.log(result);if(result.success){let item = this.fileList[fileListLen]this.fileList.splice(fileListLen, 1, Object.assign(item, {status: 'success',message: '',url: result}))fileListLen++}else{this.fileList.splice(fileListLen, 1)}}},//上传图片uploadFilePromise(url) {return new Promise((resolve, reject) => {let a = uni.uploadFile({url:'http://192.168.2.21:7001/upload', // 仅为示例,非真实的接口地址,换成自己上传图片的接口filePath: url,name: 'file',success: (uploadRes) => {setTimeout(()=>{let res = JSON.parse(uploadRes.data) //最终传给的是字符串,这里需要转换格式if(res.code == 0){this.$common.msg(res.msg)resolve({success:false,url:''})return;}resolve({success:true,url:res.data.url})},2000)}});})},//点击确认还需要些许修改fabu() {let images = []this.fileList.forEach((item) => {images.push(item.url.url)})// 其他接口request('post', '其他接口地址', {title: this.textValue,info: this.textValue,images: images.join(',')//重点是这里,需要看后台接收的类型进行更改}).then(res => {if (res.code == 1) {console.log(res);}})},}图片上传// 图片上传//删除图片deletePic(event) {this[`fileList${event.name}`].splice(event.index, 1)},// 新增图片async afterRead(event) {// 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式let lists = [].concat(event.file)let fileListLen = this[`fileList${event.name}`].lengthlists.map((item) => {this[`fileList${event.name}`].push({...item,status: 'uploading',message: '上传中'})})for (let i = 0; i < lists.length; i++) {const result = await this.uploadFilePromise(lists[i].url)console.log("结果", result);if (result.success) {let item = this.fileList[fileListLen]this.fileList.splice(fileListLen, 1, Object.assign(item, {status: 'success',message: '',url: result}))fileListLen++} else {this.fileList.splice(fileListLen, 1)}}},uploadFilePromise(url) {return new Promise((resolve, reject) => {let a = uni.uploadFile({url: config[config.env].apiUrl + '/api/common/upload', // 仅为示例,非真实的接口地址filePath: url,name: 'file',formData: {user: 'test'},success: (uploadRes) => {setTimeout(() => {let res = JSON.parse(uploadRes.data) //最终传给的是字符串,这里需要转换格式if (res.code == 0) {this.$common.msg(res.msg)resolve({success: false,url: ''})return;}console.log("图片", res);resolve({success: true,url: res.data.url})}, 1000)}});})},单图传值data(){return{payment_credentials:''//支付凭证(线下必填)}},methods: {aaa(){if (this.fileList.length > 0) {this.payment_credentials = this.fileList[0].url.url}}}图片回显

当内容进行修改时,需要先将上次上传过的图片进行显示(后台会返回图片数组),之后或许删除上次中的某种图片,也有可能会新上传一些图片。最后提交给后台的数据的图片数组是不带域名的。 整体步骤如下:

data() {return {// 图片列表fileList: [],//修改功能 需要提交的图片数组subImg:[]}},1.修改,根据id 进行查询此条数据的信息,后台返回该条数据信息,这里只说图片的事。首先对images进行处理,字符串转化为数组,之后对分割后的数组subImg进行遍历循环,把图片路径(拼接好域名之后)push到fileList数组里面,这时候页面的图片就能显示了。this.subImg = res.data.images.split(',') //字符串转化为数组this.subImg.forEach(item=>{console.log(item);this.fileList.push({url: this.$common.image(item)//拼接好域名之后push进去})})console.log(this.fileList);

2.由于修改之后提交给后台的图片数组是不带域名的,所以自定义了一个subImg数组,刚上来的时候就已经赋进去了最初始的两张图,如果删除,就根据删除的下标删除掉此数组里面的图片,每次上传新图片的时候,就把新上传后返回的图片路径push进去,这样 不管是删除还是新增都能保证sunImg数组里面的图片都是最新的,且不带域名

核心代码:

this.subImg.splice(e.index, 1)this.subImg.push(res.data.url)

具体代码:

//删除图片deletePic(e) {console.log(e);this.fileList.splice(e.index, 1)this.subImg.splice(e.index, 1) //新增删除代码},// 新增图片async afterRead(event) {// 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式let lists = [].concat(event.file)let fileListLen = this.fileList.lengthlists.map((item) => {this.fileList.push({...item,status: 'uploading',message: '上传中'})})for (let i = 0; i < lists.length; i++) {const result = await this.uploadFilePromise(lists[i].url)console.log(result);if (result.success) {let item = this.fileList[fileListLen]this.fileList.splice(fileListLen, 1, Object.assign(item, {status: 'success',message: '',url: result}))fileListLen++} else {this.fileList.splice(fileListLen, 1)}}},//上传图片uploadFilePromise(url) {return new Promise((resolve, reject) => {let a = uni.uploadFile({url: config[config.env].apiUrl + '/api/common/upload',filePath: url,name: 'file',success: (uploadRes) => {setTimeout(() => {let res = JSON.parse(uploadRes.data) //最终传给的是字符串,这里需要转换格式if (res.code == 0) {this.$common.msg(res.msg)resolve({success: false,url: ''})return;}console.log("图片", res.data.url);this.subImg.push(res.data.url) //新增添加代码resolve({success: true,url: res.data.url})}, 2000)}});})},
本文链接地址:https://www.jiuchutong.com/zhishi/283634.html 转载请保留说明!

上一篇:ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1

下一篇:绿萝怎么养(图文)(绿萝怎么样养)

  • 谈谈网站推广过程中需知注意要点(总结网站推广对产品销售的重要性)

    谈谈网站推广过程中需知注意要点(总结网站推广对产品销售的重要性)

  • 马志明相声黄大仙(魅族mx四核怎么样)(马志明相声大全 黄族民)

    马志明相声黄大仙(魅族mx四核怎么样)(马志明相声大全 黄族民)

  • 华为手机nova4e怎么没有智能辅助(华为手机nova4e怎么恢复出厂设置)

    华为手机nova4e怎么没有智能辅助(华为手机nova4e怎么恢复出厂设置)

  • soul可以隐身在线吗(soul可以隐身在派对吗)

    soul可以隐身在线吗(soul可以隐身在派对吗)

  • 京东评价自己可以删除吗(京东里面自己的评价在哪里能找到)

    京东评价自己可以删除吗(京东里面自己的评价在哪里能找到)

  • 抖音怎么不显示在线时间(抖音怎么不显示粉丝团)

    抖音怎么不显示在线时间(抖音怎么不显示粉丝团)

  • 音箱上的录音怎么录的(音响录音使用方法)

    音箱上的录音怎么录的(音响录音使用方法)

  • 对方呼叫转移怎么打通(对方呼叫转移怎么破解)

    对方呼叫转移怎么打通(对方呼叫转移怎么破解)

  • 为什么switch连不上网(为什么switch连不上蓝牙耳机)

    为什么switch连不上网(为什么switch连不上蓝牙耳机)

  • 为什么手机电量一直不变(为什么手机电量不能充到100%)

    为什么手机电量一直不变(为什么手机电量不能充到100%)

  • 小米应用商店wifi不能联网(小米应用商店wifi下载慢)

    小米应用商店wifi不能联网(小米应用商店wifi下载慢)

  • 充电只充到80正确吗(为什么充电只到80)

    充电只充到80正确吗(为什么充电只到80)

  • win7麦克风没有回声消除选项(win7麦克风没反应)

    win7麦克风没有回声消除选项(win7麦克风没反应)

  • 苹果xr闪光灯怎么关闭(苹果xr闪光灯怎么开来电闪)

    苹果xr闪光灯怎么关闭(苹果xr闪光灯怎么开来电闪)

  • 平板电脑开不开机了怎么办(平板电脑开不开机怎么办的视频教程)

    平板电脑开不开机了怎么办(平板电脑开不开机怎么办的视频教程)

  • 苹果触感灵敏怎么调(苹果触感太灵敏)

    苹果触感灵敏怎么调(苹果触感太灵敏)

  • 联想电脑开机按哪个键(联想电脑开机按F12没反应)

    联想电脑开机按哪个键(联想电脑开机按F12没反应)

  • ipone11单卡还是双卡(苹果11单卡好还是双卡)

    ipone11单卡还是双卡(苹果11单卡好还是双卡)

  • 视频没有tv标志怎么投屏

    视频没有tv标志怎么投屏

  • 微信怎么退出登录(微信怎么退出登录过的网站)

    微信怎么退出登录(微信怎么退出登录过的网站)

  • 电脑机箱前面耳机没声音怎么办?(电脑机箱前面耳机的WIN10系统没有声音怎么办)

    电脑机箱前面耳机没声音怎么办?(电脑机箱前面耳机的WIN10系统没有声音怎么办)

  • Win10系统损坏开不了机怎么办?(win10系统损坏开不了机)

    Win10系统损坏开不了机怎么办?(win10系统损坏开不了机)

  • ezSP_Px.exe是什么进程?ezSP_Px.exe是不是病毒?(exescope是什么软件)

    ezSP_Px.exe是什么进程?ezSP_Px.exe是不是病毒?(exescope是什么软件)

  • BurpSuite安装和基础使用教程(已破解)(burp怎么安装)

    BurpSuite安装和基础使用教程(已破解)(burp怎么安装)

  • 小规模纳税人税率1%政策到什么时候
  • 盖了骑缝章的合同怎么写
  • 税款已缴未入库是怎么回事
  • 支付所得税属于筹资活动吗
  • 招标代理服务费由谁支付
  • 应交税费明细表填制
  • 向个人账户汇款选项
  • 残疾人就业保障金申报时间
  • 财务报表的资产负债表怎么填
  • 资产负债表存货项目期末余额怎么算
  • 应收贷方余额怎么处理
  • 单位补缴社保会罚款吗
  • 房地产公司扣减土地出让金怎么入账?
  • 企业中由特定人员所承担的一项
  • 内部交易增值税怎么算
  • 技术研发阶段的费用如何做分录
  • 企业如何处理
  • 建筑业增值税专票抵扣后的税点是多少
  • 小微企业享受优惠
  • 小型微利企业能开多少发票
  • 委托加工物资增值税怎么计算
  • 工程施工余额怎么处理
  • 出口发票上的汇率按哪个开?
  • 驾校如何用完工百分比法确认收入?
  • 房屋租赁公司要交哪些税
  • mac怎么调整网页大小
  • 王者荣耀中太乙真人的技能有哪些?
  • 进项税额加计抵减会计分录
  • 多系统设置
  • 股票分红怎么扣税
  • thinkphp5自定义标签
  • phppregmatch
  • 车间耗材管理办法
  • 商贸企业小规模税率是多少
  • coded language
  • 使用二氧化碳灭火器时人应该站在什么位置
  • 可以跨城租车吗
  • 三万个字多久写完
  • vue3 element ui
  • php数据库设计
  • web前端面试题2021及答案
  • 图像风格迁移网站
  • phpwebsocket框架
  • 保险公司的应收账款有哪些
  • hive.
  • 社保单位当月应缴月报查询
  • mongodb自增主键
  • 住宿补贴怎么算
  • 企业注销未抵扣完进项税怎么处理
  • 企业所得税汇算表
  • 递延所得税的会计核算
  • 弃置费用预计负债的会计处理
  • 建设工程项目设计阶段的投资控制方法有哪些
  • 工程类的增值税
  • 电费发票开据后如何入帐?
  • 本月未过账的凭证怎么写
  • 联营公司是关联方吗
  • 机票改签手续费有报销凭证吗
  • 建筑劳务公司的会计账务处理
  • 一般纳税人先确认收入吗
  • 个人报销费用怎么做分录
  • 银行日记账如何制做
  • 电脑算固定资产哪一类
  • winxp怎么做系统
  • win10开机cpu占用率100%
  • linux怎么查看硬件信息
  • c++服务器与客户端连接
  • com.android.phone无响应
  • perl的$_
  • nodejs调试指南
  • android fragmentation
  • 事件绑定js
  • android如何防止js注入
  • python条件怎么算合法
  • 税务局约谈严重吗
  • 新疆油田在哪个地方
  • 上海各区税务大厅
  • 上海个体户纳税标准
  • 江苏税务缴费小程序
  • 北京税务代办
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号