当前位置:网站首页>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 .
边栏推荐
- 艾芬医生事件解析
- USBCAN分析仪的配套CAN和CANFD综合测试软件LKMaster软件解决工程师CAN总线测试难题
- 京东与腾讯续签三年战略合作协议;起薪涨至26万元,韩国三星SK争相加薪留住半导体人才;Firefox 102 发布|极客头条
- Introduction and example of template method mode
- 机器学习工作要求研究生吗?
- On several key issues of digital transformation
- Uniapp routing uni simple router
- Modify the name of the launched applet
- Qsort function and Simulation Implementation of qsort function
- Pytorch quantitative practice (2)
猜你喜欢

Usbcan analyzer's supporting can and canfd comprehensive test software lkmaster software solves engineers' can bus test problems

jupyterbook 清空控制台输出

本地浏览器打开远程服务器上的Jupyter Notebook/Lab以及常见问题&设置

PostgreSQL存储结构浅析

《Dynamic Routing Between Capsules》论文学习总结

興奮神經遞質——穀氨酸與大腦健康

Multi table operation - foreign key constraint

Go Web 编程入门: 一探优秀测试库 GoConvey

JD and Tencent renewed the three-year strategic cooperation agreement; The starting salary rose to 260000 yuan, and Samsung sk of South Korea scrambled for a raise to retain semiconductor talents; Fir

Prediction and regression of stacking integrated model
随机推荐
Prediction and regression of stacking integrated model
1-15 nodemon
根据肠道微生物组重新思考健康饮食
Anti leakage family photo in attack and defense drill
Pytorch quantitative practice (2)
[untitled] first time to participate in CSDN activities
1-21 jsonp interface
Best wishes for Lao Wu's party
Go Web 编程入门: 一探优秀测试库 GoConvey
全面认识痛风:症状、风险因素、发病机理及管理
A comprehensive understanding of gout: symptoms, risk factors, pathogenesis and management
Do machine learning jobs require graduate students?
Open the jupyter notebook/lab and FAQ & settings on the remote server with the local browser
Do a scrollbar thinking
盘点华为云GaussDB(for Redis)六大秒级能力
Installing jupyter notebook under Anaconda
The Jenkins download Plug-in can't be downloaded. Solution
Is machine learning suitable for girls?
Neurotransmetteurs excitateurs - glutamate et santé cérébrale
jupyter notebook/lab 切换conda环境