first of all, we need to build a koa2 environment, so I won"t say much about it here.
next you need to load koa-multer:
npm install koa-multer-- save
the specific code is as follows:
first is the js section:
const multer = require("koa-multer");//koa-multer
//
//
var storage = multer.diskStorage({
//
destination: function (req, file, cb) {
cb(null, "public/uploads/")
},
//
filename: function (req, file, cb) {
var fileFormat = (file.originalname).split(".");
cb(null,Date.now() + "." + fileFormat[fileFormat.length - 1]);
}
})
//
var upload = multer({ storage: storage });
//
router.post("/upload", upload.single("file"), async (ctx, next) => {
conso.log("debugger");
ctx.body = {
filename: ctx.req.file.filename//
}
})
there is no problem with saving pictures. The code behind the post request cannot use debugger, to ask for advice, or other image upload methods can also be used