egg怎么把静态文件里的图片路径保存到mysql
时间: 2023-12-19 18:05:54 浏览: 97
要把静态文件里的图片路径保存到 MySQL 数据库中,可以先将图片上传到服务器上的某个目录下,然后把该图片的路径保存到数据库中。
具体实现步骤如下:
1. 在 egg 中配置静态资源目录,例如:
```
// config/config.default.js
exports.static = {
prefix: '/public/',
dir: path.join(appInfo.baseDir, 'app/public')
};
```
2. 在前端页面中上传图片并将其保存到服务器上的某个目录下,例如:
```
<input type="file" id="upload-file" name="upload-file">
<script>
var input = document.getElementById('upload-file');
var file = input.files[0];
var formData = new FormData();
formData.append('file', file);
var xhr = new XMLHttpRequest();
xhr.open('POST', '/upload');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
console.log(xhr.responseText);
}
};
xhr.send(formData);
</script>
```
3. 在 egg 中编写上传图片的路由处理函数,并将图片路径保存到 MySQL 数据库中,例如:
```
// app/controller/upload.js
const fs = require('fs');
const path = require('path');
const Controller = require('egg').Controller;
class UploadController extends Controller {
async index() {
const { ctx } = this;
const file = ctx.request.files[0];
const filename = file.filename;
const targetPath = path.join(this.config.static.dir, filename);
const result = await new Promise((resolve, reject) => {
const stream = fs.createWriteStream(targetPath);
stream.on('finish', () => {
resolve({
code: 0,
msg: '上传成功',
data: {
url: `${this.config.static.prefix}${filename}`
}
});
});
stream.on('error', (err) => {
reject({
code: -1,
msg: '上传失败',
data: err
});
});
file.stream.pipe(stream);
});
if (result.code === 0) {
const url = result.data.url;
const { affectedRows } = await ctx.app.mysql.insert('images', { url });
if (affectedRows === 1) {
ctx.body = {
code: 0,
msg: '保存成功',
data: url
};
} else {
ctx.body = {
code: -1,
msg: '保存失败',
data: null
};
}
} else {
ctx.body = result;
}
}
}
module.exports = UploadController;
```
在该路由处理函数中,首先将上传的图片保存到服务器上的某个目录下,然后将图片的路径保存到 MySQL 数据库中,并返回图片的访问路径给前端页面。
注意:在实际开发中,需要对上传的文件进行安全性检查,例如文件类型、大小等。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![egg](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)