当前位置:网站首页>uniapp实现从本地上传头像并显示,同时将头像转化为base64格式存储在mysql数据库中
uniapp实现从本地上传头像并显示,同时将头像转化为base64格式存储在mysql数据库中
2022-07-06 16:43:00 【ChangYan.】
想实现的功能为:从本地选择一张图片作为头像,然后将该图片以base64格式存储在数据库中。
效果如下:
- 这个前端页面使用了插件商城里免费的前辈分享的模板:https://ext.dcloud.net.cn/plugin?id=2027
对于html部分点击获取头像前辈已经完成好,下图中的此部分无需更改。
- 下一步要做的就是将上传的头像转化为base64格式
avatarChoose() {
let that = this;
uni.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: function (res) {
var tempFilePaths = res.tempFilePaths;
pathToBase64(tempFilePaths[0]) //图像转base64工具
.then(base64 => {
console.log(base64);
console.log(typeof(base64));
that.avatar = base64; //将文件转化为base64并显示
that.avatarUpload(base64); //同时将头像上传至数据库进行存储
})
.catch(error => {
console.error(error)
})
}
});
},
这里使用了pathToBase64
这个工具,所以需要去插件市场里下载并引入到项目中https://ext.dcloud.net.cn/plugin?id=123
我在此处输出了一下转化结果和结果的类型
可以看到控制台的显示,转化成功,而且base64的类型是String字符串
- 现在已经能将刚刚上传的头像显示出来了,接下来就是将该图片存储在数据库中。
在上段代码的最后一步调用了avatarUpload函数
所以添加代码,将数据传至后端:
avatarUpload(avatar) {
let that = this;
var res = {
"userId": that.userId, //对应于哪个用户上传头像
"avatar": avatar //转化成功的base64
}
uni.request({
url: "http://localhost:8080/user/avatarUpload", //传到后端
data: JSON.stringify(res), //要传输的数据
header: {
token: that.token },
method: "PUT",
success(r) {
console.log(r);
if (r.data.message == "操作成功") {
uni.showToast({
title: "设置成功"
});
} else {
uni.showToast({
title: r.data.message,
icon: "error",
});
}
},
fail() {
uni.showToast({
title: "网络错误",
icon: "error",
});
},
});
},
对于后端,先要配置数据库
在表中添加avatar属性
对于这个avatar属性的类型,我设置的是mediumtext,防止溢出,就是说base64太长了,害怕varchar类性存不下。
(其实我最初想的是既然是String字符串,那就设置成varchar,然后百度了一下说varchar的最大长度可以到65535,所以那里的长度我就写成了65535,结果一回车确定,它就自动变成了mediumtext类型,可以说很智能了…)然后在类中添加avatar属性
最后后端的Java在controller处进行接收
@PreAuthorize(value = "hasAuthority('sys:put')")
@ApiOperation("上传头像" )
@PutMapping("/avatarUpload")
public ResponseResult avatarUpload(@RequestBody @Valid User user) {
int res = userService.updateUserById(user);
Map<String, Integer> map = new HashMap<>();
map.put("影响行数",res);
return new ResponseResult<Map>(200,"操作成功", map);
}
这里因为前端只传了userId和avatar,所以调用updateUserById
函数完成头像到数据库的存储。最后可以看到运行结果:
存储成功!
边栏推荐
- Penetration test --- database security: detailed explanation of SQL injection into database principle
- "Latex" Introduction to latex mathematical formula "suggestions collection"
- pytest多进程/多线程执行测试用例
- 在docker中快速使用各个版本的PostgreSQL数据库
- Encryption algorithm - password security
- Building lease management system based on SSM framework
- What is web penetration testing_ Infiltration practice
- Introduction to GPIO
- vector的使用方法_vector指针如何使用
- How to use vector_ How to use vector pointer
猜你喜欢
随机推荐
MATLIB reads data from excel table and draws function image
Three application characteristics of immersive projection in offline display
Leecode brush questions record sword finger offer 43 The number of occurrences of 1 in integers 1 to n
基于SSM框架实现的房屋租赁管理系统
TypeScript中使用类型别名
Google, Baidu and Yahoo are general search engines developed by Chinese companies_ Baidu search engine URL
DAY FOUR
How can computers ensure data security in the quantum era? The United States announced four alternative encryption algorithms
Wind chime card issuing network source code latest version - commercially available
Competition between public and private chains in data privacy and throughput
PXE server configuration
【向量检索研究系列】产品介绍
App general function test cases
rancher集成ldap,实现统一账号登录
What can the interactive slide screen demonstration bring to the enterprise exhibition hall
Oracle中使用包FY_Recover_Data.pck来恢复truncate误操作的表
The largest single investment in the history of Dachen was IPO today
Rails 4 asset pipeline vendor asset images are not precompiled
Encryption algorithm - password security
Designed for decision tree, the National University of Singapore and Tsinghua University jointly proposed a fast and safe federal learning system