当前位置:网站首页>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怎么上传二进制图片”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
边栏推荐
- Description of the latest RTSP address rules for Hikvision camera, NVR, streaming media server, playback and streaming [easy to understand]
- On inline function
- MySQL简介、详细安装步骤及使用 | 黑马程序员
- Go learning notes
- MySQL:SQL概述及数据库系统介绍 | 黑马程序员
- 第299场
- Big God explains open source buff gain strategy live broadcast
- Is it safe to open an account for online stock trading!?
- jfinal中如何使用过滤器监控Druid监听SQL执行?
- Lumiprobe biotin phosphimide (hydroxyproline) instructions
猜你喜欢

B_QuRT_User_Guide(33)

Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing

On the charm of code language

Lumiprobe无铜点击化学解决方案

注册设备监理师难考吗,和监理工程师有什么关系?

1、生成对抗网络入门

Lumiprobe protein quantitation - qudye Protein Quantitation Kit

SQL优化

Study on lumiprobe modified triphosphate biotin-11-utp

centos——开启/关闭oracle
随机推荐
jfinal中如何使用过滤器监控Druid监听SQL执行?
断点续传和下载原理分析
最新海康摄像机、NVR、流媒体服务器、回放取流RTSP地址规则说明[通俗易懂]
Encoding type of Perl conversion file
北京大学ACM Problems 1004:Financial Management
请问海量数据如何去取最大的K个
Go learning notes
北京大学ACM Problems 1002:487-3279
Analysis of breakpoint continuation and download principle
QT qstringlist usage
PHP require/include 区别
Jenkins打包拉取不到最新的jar包
Halcon knowledge: check the measurement objects [1]
二叉查找树(一) - 概念与C语言实现
Summary of personal work of 21 groups in the first week of summer training
杰理之用测试盒配对软件修改注意点【篇】
C文件指针
动态样式绑定--style 和 class
What are database OLAP and OLTP? Same and different? Applicable scenarios
哈夫曼树(一)基本概念与C语言实现