当前位置:网站首页>How to upload binary pictures in uniapp
How to upload binary pictures in uniapp
2022-06-30 21:59:00 【Yisu cloud】
uniapp How to upload binary pictures
This article mainly introduces “uniapp How to upload binary pictures ” Knowledge about , Xiaobian shows you the operation process through practical cases , The operation method is simple and fast , Practical , Hope this article “uniapp How to upload binary pictures ” The article can help you solve problems .
functional requirement :
The front end selects the local file , Display the selected file on the interface for preview , You can select four at the same time for preview .
Ideas as follows :
The front end selects local png、jpg、 Wait for the format of the picture , Send the picture to the back end in binary form The server , The back end processes binary images , Return to the front-end a server link online picture , In the browser, you can open the link to access . Then the front end renders the image link on the page for preview .
First
Let's see uniapp Official documents of :https://uniapp.dcloud.io/api/media/image?id=chooseimage
It looks something like this
First write a simulated demo
1: First I did colorUI Framework , Introduce... Into the project
stay page Under the vue The file import
@import "../../colorui/main.css";@import "../../colorui/icon.css";
thus , There is no need to write any style , Just use what you have written .
<template> <view> <form> <view class="cu-bar bg-white margin-top"> <view class="action"> Image upload </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, // Default 9 sizeType: ['original', 'compressed'], // You can specify whether it is original or compressed , By default, both of them have sourceType: ['album'], // Select from the 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 }); }, // Delete DelImg(e) { uni.showModal({ title: ' Delete ', content: ' Are you sure you want to delete this picture ?', cancelText: ' Cancel ', confirmText: ' Delete ', 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> The effect is this
Each time you select a picture, it will be displayed on the page , I have set up a maximum of four options here , Picture links use temporary blob, Next, you need to use the interface provided by the backend partner , I passed my local binary file to him .
stay chooseImage After selecting the picture , Write a successful callback function , Add an image uploading method in the return function uploadFile, Add... To the method url, Equal parameter .

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] } }); } If the request is successful
Then return a picture link
After adding the interface Of ,demo as follows :
<template> <view> <form> <view class="cu-bar bg-white margin-top"> <view class="action"> Image upload </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, // Default 9 sizeType: ['original', 'compressed'], // You can specify whether it is original or compressed , By default, both of them have sourceType: ['album'], // Select from the 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 }); }, // Delete DelImg(e) { uni.showModal({ title: ' Delete ', content: ' Are you sure you want to delete this picture ?', cancelText: ' Cancel ', confirmText: ' Delete ', 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>
Upload picture effects

About “uniapp How to upload binary pictures ” That's all for , Thanks for reading . If you want to know more about the industry , You can pay attention to the Yisu cloud industry information channel , Xiaobian will update different knowledge points for you every day .
边栏推荐
- 1-14 express managed static resources
- 攻防演练中的防泄露全家福
- Sqlserver gets the data of numbers, Chinese and characters in the string
- vim 常用快捷键
- The programmer's girlfriend gave me a fatigue driving test
- Microservice link risk analysis
- 《安富莱嵌入式周报》第270期:2022.06.13--2022.06.19
- 1-17 express Middleware
- How to use data sets in machine learning?
- [untitled] first time to participate in CSDN activities
猜你喜欢

Uniapp life cycle / route jump

1. Summary of wechat applet page Jump methods; 2. the navigateto stack does not jump to the tenth floor

Clickhouse Native Monitoring item, System table Description

Study summary of dynamic routing between capsules

Multi table operation - foreign key constraint

Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)

Best wishes for Lao Wu's party

USBCAN分析仪的配套CAN和CANFD综合测试软件LKMaster软件解决工程师CAN总线测试难题

pytorch geometric torch-scatter和torch-sparse安装报错问题解决

Random talk about Clickhouse join
随机推荐
Five years after graduation, I wondered if I would still be so anxious if I hadn't taken the test
Is machine learning suitable for girls?
Pytorch quantitative perception training (qat) steps
Is there a shortage? No need to download the free online resources! 2022 favorites must have it!
WinDbg debugging tool introduction
请问,启牛证券开户,可以开户吗?安全吗?你想要的答案全在这里
1-1 basic concepts of database
介绍一款|用于多组学整合和网络可视化分析的在线平台
1-17 express Middleware
Look at the top 10 capabilities of alicloud cipu
The programmer's girlfriend gave me a fatigue driving test
Stimulate new kinetic energy to develop digital economy in multiple places
Stinky tofu made by Grandma
吴恩达的机器学习适合入门吗?
[introduction to MySQL] the first conversation · first time in the "database" Mainland
1-12 初步认识Express
Clickhouse distributed table engine
Alibaba Kube eventer MySQL sink simple usage record
1-11 create online file service
Uniapp rich text editor