当前位置:网站首页>Uniapp uploads and displays avatars locally, and converts avatars into Base64 format and stores them in MySQL database
Uniapp uploads and displays avatars locally, and converts avatars into Base64 format and stores them in MySQL database
2022-07-07 00:20:00 【ChangYan.】
The function you want to realize is : Choose a picture locally as the avatar , Then put the picture as base64 The format is stored in the database .
The effect is as follows :
- This front-end page uses the free template shared by predecessors in the plug-in mall :https://ext.dcloud.net.cn/plugin?id=2027

about html Some Click to get avatars, master has finished , There is no need to change this part in the following figure .
- The next step is to convert the uploaded avatar into base64 Format

avatarChoose() {
let that = this;
uni.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: function (res) {
var tempFilePaths = res.tempFilePaths;
pathToBase64(tempFilePaths[0]) // Image to base64 Tools
.then(base64 => {
console.log(base64);
console.log(typeof(base64));
that.avatar = base64; // Convert the document to base64 And display
that.avatarUpload(base64); // At the same time, upload the avatar to the database for storage
})
.catch(error => {
console.error(error)
})
}
});
},
It's used here pathToBase64 This tool , So you need to download it in the plug-in market and introduce it into the project https://ext.dcloud.net.cn/plugin?id=123

Here I output the conversion result and the type of the result 
You can see the display of the console , Successful transformation , and base64 The type is String character string

- Now the avatar just uploaded can be displayed , The next step is to store the image in the database .
In the last step of the previous code, I called avatarUpload function
So add code , Send data to the back end :
avatarUpload(avatar) {
let that = this;
var res = {
"userId": that.userId, // Corresponding to which user uploads the avatar
"avatar": avatar // Successful transformation base64
}
uni.request({
url: "http://localhost:8080/user/avatarUpload", // To back end
data: JSON.stringify(res), // Data to transfer
header: {
token: that.token },
method: "PUT",
success(r) {
console.log(r);
if (r.data.message == " Successful operation ") {
uni.showToast({
title: " Set up the success "
});
} else {
uni.showToast({
title: r.data.message,
icon: "error",
});
}
},
fail() {
uni.showToast({
title: " Network error ",
icon: "error",
});
},
});
},
For the back end , First, configure the database
Add... To the table avatar attribute
For this avatar Type of attribute , What I set up is mediumtext, Prevent overflow , That is to say base64 Is too long. , Fear varchar Class cannot survive .
( In fact, what I initially thought was that since it was String character string , Then set it to varchar, Then Baidu said varchar The maximum length of can reach 65535, So I wrote the length there 65535, The result is confirmed as soon as you enter , It automatically becomes mediumtext type , It can be said to be very intelligent …)Then add... To the class avatar attribute

Finally, the backend Java stay controller Receive at

@PreAuthorize(value = "hasAuthority('sys:put')")
@ApiOperation(" Upload your avatar " )
@PutMapping("/avatarUpload")
public ResponseResult avatarUpload(@RequestBody @Valid User user) {
int res = userService.updateUserById(user);
Map<String, Integer> map = new HashMap<>();
map.put(" Number of rows affected ",res);
return new ResponseResult<Map>(200," Successful operation ", map);
}
Because the front end only transmits userId and avatar, So call updateUserById Function to store the avatar in the database . Finally, you can see the running results :
Storage success !
边栏推荐
- GEO数据挖掘(三)使用DAVID数据库进行GO、KEGG富集分析
- Leecode brush questions record sword finger offer 43 The number of occurrences of 1 in integers 1 to n
- What is web penetration testing_ Infiltration practice
- Use type aliases in typescript
- [CVPR 2022] semi supervised object detection: dense learning based semi supervised object detection
- app通用功能測試用例
- Rider离线使用Nuget包的方法
- TypeScript中使用类型别名
- Typescript incremental compilation
- Designed for decision tree, the National University of Singapore and Tsinghua University jointly proposed a fast and safe federal learning system
猜你喜欢

AVL树到底是什么?

刘永鑫报告|微生物组数据分析与科学传播(晚7点半)

MATLIB reads data from excel table and draws function image

2022/2/10 summary

Pytest multi process / multi thread execution test case

DAY ONE

陀螺仪的工作原理

Liuyongxin report | microbiome data analysis and science communication (7:30 p.m.)

ldap创建公司组织、人员

Designed for decision tree, the National University of Singapore and Tsinghua University jointly proposed a fast and safe federal learning system
随机推荐
iMeta | 华南农大陈程杰/夏瑞等发布TBtools构造Circos图的简单方法
Interface joint debugging test script optimization v4.0
DAY SIX
华为mate8电池价格_华为mate8换电池后充电巨慢
Tourism Management System Based on jsp+servlet+mysql framework [source code + database + report]
[vector retrieval research series] product introduction
Things like random
Leecode brush questions record interview questions 32 - I. print binary tree from top to bottom
[CVPR 2022] semi supervised object detection: dense learning based semi supervised object detection
The programmer resigned and was sentenced to 10 months for deleting the code. Jingdong came home and said that it took 30000 to restore the database. Netizen: This is really a revenge
okcc呼叫中心的订单管理时怎么样的
专为决策树打造,新加坡国立大学&清华大学联合提出快速安全的联邦学习新系统
DAY FIVE
2021 SASE integration strategic roadmap (I)
基于GO语言实现的X.509证书
刘永鑫报告|微生物组数据分析与科学传播(晚7点半)
Devops can help reduce technology debt in ten ways
1000字精选 —— 接口测试基础
在docker中快速使用各个版本的PostgreSQL数据库
PostgreSQL高可用之repmgr(1主2从+1witness)+Pgpool-II实现主从切换+读写分离