当前位置:网站首页>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 !
边栏推荐
- pinia 模块划分
- DevOps可以帮助减少技术债务的十种方式
- 2021 SASE integration strategic roadmap (I)
- Matplotlib draws a histogram and adds values to the graph
- "Latex" Introduction to latex mathematical formula "suggestions collection"
- [2022 the finest in the whole network] how to test the interface test generally? Process and steps of interface test
- 《LaTex》LaTex数学公式简介「建议收藏」
- PostgreSQL uses pgpool II to realize read-write separation + load balancing
- GPIO简介
- How rider uses nuget package offline
猜你喜欢

48 page digital government smart government all in one solution

System activity monitor ISTAT menus 6.61 (1185) Chinese repair

互动滑轨屏演示能为企业展厅带来什么

37页数字乡村振兴智慧农业整体规划建设方案

How rider uses nuget package offline

2022年PMP项目管理考试敏捷知识点(9)

Core knowledge of distributed cache

Automatic test tool katalon (WEB) test operation instructions

After leaving a foreign company, I know what respect and compliance are

一图看懂对程序员的误解:西方程序员眼中的中国程序员
随机推荐
DevOps可以帮助减少技术债务的十种方式
48页数字政府智慧政务一网通办解决方案
从外企离开,我才知道什么叫尊重跟合规…
Testers, how to prepare test data
PostgreSQL使用Pgpool-II实现读写分离+负载均衡
How about the order management of okcc call center
web渗透测试是什么_渗透实战
48 page digital government smart government all in one solution
Why should a complete knapsack be traversed in sequence? Briefly explain
Racher integrates LDAP to realize unified account login
一图看懂对程序员的误解:西方程序员眼中的中国程序员
SuperSocket 1.6 创建一个简易的报文长度在头部的Socket服务器
Imeta | Chen Chengjie / Xia Rui of South China Agricultural University released a simple method of constructing Circos map by tbtools
自动化测试工具Katalon(Web)测试操作说明
What can the interactive slide screen demonstration bring to the enterprise exhibition hall
沉浸式投影在线下展示中的三大应用特点
Data operation platform - data collection [easy to understand]
kubernetes部署ldap
Pdf document signature Guide
Interface joint debugging test script optimization v4.0