要在node.js中實現KindEditor的圖片上傳功能,可以使用以下步驟:
npm install express multer
const express = require('express');
const multer = require('multer');
const router = express.Router();
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads/') // 指定圖片上傳的目錄
},
filename: function (req, file, cb) {
cb(null, file.originalname) // 使用原始文件名作為上傳后的文件名
}
})
const upload = multer({ storage: storage });
router.post('/upload', upload.single('file'), (req, res) => {
const file = req.file;
res.json({
url: `/uploads/${file.filename}`
});
});
module.exports = router;
const express = require('express');
const app = express();
const uploadRouter = require('./routes/upload');
app.use('/api', uploadRouter);
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
以上是使用node.js實現KindEditor圖片上傳功能的基本步驟,你可以根據具體需求進行適當的修改和擴展。