avatar data

This commit is contained in:
satori 2022-01-30 17:36:22 +08:00
parent 5fc803a9b2
commit 4b73a7ea08
2 changed files with 11 additions and 26 deletions

View File

@ -179,22 +179,20 @@ fetch('/user/ApSXNLoUy', {
#### 上传头像
实际分为两步,
第一步先上传附件到自己的账户
```html
<!DOCTYPE html>
<input type="file" name="photos", multiple, onchange="upload()"/>
<input type="file" name="photos", accept="image/*", multiple, onchange="upload($event)"/>
<script>
function upload() {
let myForm = new FormData();
let files = document.querySelector("[type=file]").files;
files.map(item => myForm.append("photos", item))
function upload(event) {
let data = new FormData();
//let files = document.querySelector("[type=file]").files;
//files.map(item => data.append("photos", item))
data.append("img", event.target.files[0]);
fetch('/user/ApSXNLoUy', {
method: 'POST',
headers: { 'Content-Type': 'multipart/form-data'},
body: myForm
body: data
}).then(Response => Response.json()).then(data => {
console.log(data)
})
@ -202,20 +200,6 @@ function upload() {
</script>
```
第二步修改自己的头像路径为返回的图像路径, (!注意此处未作安全检查)
```javascript
fetch('/user/ApSXNLoUy', {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
avatar: '/xxxx.jpg'
}),
}).then(Response => Response.json()).then(data => {
console.log(data)
})
```
#### 删除用户

View File

@ -396,15 +396,16 @@ const uploadavatar = function (req, res) {
if (!list[0]) return res.status(400).send('未获得图像')
let avatar = '/data/file/' + list[0].newFilename
let query = { _id: req.session.account.uid }
let data = {
$addToSet: { file: { $each: list } }, // 保存记录
$set: { avatar: '/data/file/' + list[0].newFilename }, // 替换头像
$addToSet: { file: { $each: list } }, // 保存记录
$set: { avatar }, // 替换头像
}
db('user').update(query, data, (err, count) => {
if (!count) return res.status(500).send('附件挂载对象失败')
res.json(list[0]) // 返回唯一图像
res.json({ ...list[0], avatar }) // 返回唯一图像
})
})