feat: 添加上传文件接口
This commit is contained in:
1 parent
59dbbb288c
commit
28ee6daab4
4 files changed
+36
-12
No files matched your search
+4
-6
@@ -3,22 +3,20 @@ import fs from "fs";
|
||||
import multer from "multer";
|
||||
import { api, staticPath } from "../config.js";
|
||||
|
||||
const api_upload = api.upload
|
||||
|
||||
const storage = multer.memoryStorage();
|
||||
const upload = multer({ storage: storage }).single('file');
|
||||
|
||||
export default function useUploadApi(app) {
|
||||
app.post(api_upload, upload, (req, res) => {
|
||||
app.post(api.upload, upload, (req, res) => {
|
||||
// 必须这样传,不然中文乱码
|
||||
const fileName = req.body.fileName
|
||||
|
||||
const decodedPath = decodeURIComponent(req.path).substring(api_upload.length - 1);
|
||||
const decodedPath = decodeURIComponent(req.path).substring(api.upload.length - 1);
|
||||
const savePath = path.join(staticPath, decodedPath, fileName)
|
||||
// 覆盖写or追加写
|
||||
const oprateType = req.body.oprateType === 'write' ? 'write' : 'append'
|
||||
const operateType = req.body.operateType === 'write' ? 'write' : 'append'
|
||||
const data = req.file.buffer
|
||||
if (oprateType === 'write') {
|
||||
if (operateType === 'write') {
|
||||
// 覆盖写入
|
||||
fs.writeFile(savePath, data, (err) => {
|
||||
if (err) {
|
||||
|
||||
Reference in new issue
Block a user