当前位置:网站首页>uniapp怎么上传二进制图片
uniapp怎么上传二进制图片
2022-06-30 20:19:00 【亿速云】
uniapp怎么上传二进制图片
这篇文章主要介绍“uniapp怎么上传二进制图片”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“uniapp怎么上传二进制图片”文章能帮助大家解决问题。
功能需求:
前端选择本地文件,将选择好的文件显示在界面上进行预览,可同时选择四张进行预览。
思路如下:
前端选择本地的png、jpg、等格式的图片,将图片以二进制的形式传到后端服务器,后端对二进制图片进行处理,返回给前端一个服务器链接在线图片,在浏览器就可以打开链接访问的那种。然后前端将这个图片链接渲染在页面进行预览。
首先
我们看一下uniapp的官方文档:https://uniapp.dcloud.io/api/media/image?id=chooseimage
大概是这样的
先写一个模拟的demo
1:首先我是是用了colorUI的框架,在项目里面引入
在page底下的vue文件引入
@import "../../colorui/main.css";@import "../../colorui/icon.css";
这样一来,就不需要写什么样式了,直接使用写好的就行了。
<template> <view> <form> <view class="cu-bar bg-white margin-top"> <view class="action"> 图片上传 </view> <view class="action"> {{imgList.length}}/4 </view> </view> <view class="cu-form-group"> <view class="grid col-4 grid-square flex-sub"> <view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]"> <image :src="imgList[index]" mode="aspectFill"></image> <view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index"> <text class='cuIcon-close'></text> </view> </view> <view class="solids" @tap="ChooseImage" v-if="imgList.length<4"> <text class='cuIcon-cameraadd'></text> </view> </view> </view> </form> </view></template><script> export default { data() { return { imgList: [], // modalName: null, }; }, methods: { ChooseImage() { uni.chooseImage({ count: 4, //默认9 sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有 sourceType: ['album'], //从相册选择 success: (res) => { if (this.imgList.length != 0) { this.imgList = this.imgList.concat(res.tempFilePaths) } else { this.imgList = res.tempFilePaths } } }); }, ViewImage(e) { uni.previewImage({ urls: this.imgList, current: e.currentTarget.dataset.url }); }, //删除 DelImg(e) { uni.showModal({ title: '删除', content: '确定要删除这张图片?', cancelText: '取消', confirmText: '删除', success: res => { if (res.confirm) { this.imgList.splice(e.currentTarget.dataset.index, 1) } } }) }, } }</script> <style>@import "../../colorui/main.css";@import "../../colorui/icon.css";.cu-form-group .title { min-width: calc(4em + 15px);}</style>效果是这样的
每次选完图片之后显示在页面上,我这里设置了最多可以选择四张,图片链接使用了临时的blob,接下来就要使用后端小伙伴给的接口,将自己本地的二进制文件传给他了。
在chooseImage选择好图片之后,写一个成功的回调函数,在回到函数里面添加一个图片上传的方法uploadFile,在方法里面添加url,等参数。

success: (res) => { const tempFilePaths = res.tempFilePaths; const uploadTask = uni.uploadFile({ url: 'http://47.xxx.xxx.78:8088/chemApp/work/upload.action', filePath: tempFilePaths[0], name: 'file', success: function(uploadFileRes) { console.log(uploadFileRes); _this.imgList = [..._this.imgList, uploadFileRes.data] } }); }若是请求成功
则返回一个图片链接
添加接口之后 的,demo如下:
<template> <view> <form> <view class="cu-bar bg-white margin-top"> <view class="action"> 图片上传 </view> <view class="action"> {{imgList.length}}/4 </view> </view> <view class="cu-form-group"> <view class="grid col-4 grid-square flex-sub"> <view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="item"> <image v-if="item" :src="item" mode="aspectFill"></image> <view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index"> <text class='cuIcon-close'></text> </view> </view> <view class="solids" @tap="ChooseImage" v-if="imgList.length<4"> <text class='cuIcon-cameraadd'></text> </view> </view> </view> </form> </view></template><script> export default { data() { return { imgList: [], // modalName: null, }; }, methods: { ChooseImage() { const _this = this uni.chooseImage({ count: 4, //默认9 sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有 sourceType: ['album'], //从相册选择 success: (res) => { const tempFilePaths = res.tempFilePaths; const uploadTask = uni.uploadFile({ url : 'http://47.xxx.xxx.78:8088/chemApp/work/upload.action', filePath: tempFilePaths[0], name: 'file', success: function (uploadFileRes) { console.log(uploadFileRes); _this.imgList = [..._this.imgList, uploadFileRes.data] } }); } }); }, ViewImage(e) { uni.previewImage({ urls: this.imgList, current: e.currentTarget.dataset.url }); }, //删除 DelImg(e) { uni.showModal({ title: '删除', content: '确定要删除这张图片?', cancelText: '取消', confirmText: '删除', success: res => { if (res.confirm) { this.imgList.splice(e.currentTarget.dataset.index, 1) } } }) }, } }</script> <style>@import "../../colorui/main.css";@import "../../colorui/icon.css";.cu-form-group .title { min-width: calc(4em + 15px);}</style>
上传图片效果

关于“uniapp怎么上传二进制图片”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
边栏推荐
猜你喜欢

杰理之触摸按键识别流程【篇】

The Commission is so high that everyone can participate in the new programmer's partner plan

Lumiprobe染料 NHS 酯丨BDP FL NHS 酯研究

基于开源流批一体数据同步引擎ChunJun数据还原—DDL解析模块的实战分享

【数字IC应届生职业规划】Chap.1 IC行业产业链概述及代表企业大厂汇总

MFC界面库BCGControlBar v33.0 - 桌面警报窗口、网格控件升级等

Document contains & conditional competition

Basic concepts of tree

Study on lumiprobe dye NHS ester BDP FL NHS ester
![[1175. prime number arrangement]](/img/f2/d427db03da151786ea1dfb7a76328a.png)
[1175. prime number arrangement]
随机推荐
On inline function
数据库 OLAP、OLTP是什么?相同和不同?适用场景
请指教在线开户需要什么银行卡?另外想问,现在在线开户安全么?
杰理之关于长按复位【篇】
Scene 299
centos——开启/关闭oracle
Jenkins can't pull the latest jar package
Installation and use of securecrtportable
Maya house modeling
最新海康摄像机、NVR、流媒体服务器、回放取流RTSP地址规则说明[通俗易懂]
大神详解开源 BUFF 增益攻略丨直播
Amazon restricts LGBTQ related search and product sales in the United Arab Emirates
A complete collection of vulnerability scanning tools. Mom doesn't have to worry that I won't find any more vulnerabilities
DEX file parsing - Method_ IDS resolution
unittest自动测试多个用例时,logging模块重复打印解决
北京大学ACM Problems 1002:487-3279
B_QuRT_User_Guide(32)
1、生成对抗网络入门
SQL必需掌握的100个重要知识点:创建和操纵表
jfinal中如何使用过滤器监控Druid监听SQL执行?