首先在node下面下载Express以及multer
npm i express
npm i multer
然后写代码
const Express = require("express");
const app = new Express;
const fs = require("fs");
const multer = require("multer");
var upload = multer({ dest: 'uploads/' })//当前目录下建立文件夹uploads
app.post('/uploads', upload.single('files'), function (req, res, next) { res.send("12") });
app.listen(3000, () => { console.log(123) });
app.get("/form", function (req, res) { res.send(fs.readFileSync("./form.html", "utf8")) });
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form method="POST" action="/uploads" enctype="multipart/form-data">
<input type="file" value="上传" name="files">
<input type="submit">
</form>
</body>
</html>
再启动node就可以实现图片上传,接下来是多个图片上传
const Express = require("express");
const app = new Express;
const fs = require("fs");
const multer = require("multer");
var upload = multer({ dest: 'uploads/' })//当前目录下建立文件夹uploads
app.post('/uploads', upload.array('files',2), function (req, res, next) { res.send("12") });
app.listen(3000, () => { console.log(123) });
app.get("/form", function (req, res) { res.send(fs.readFileSync("./form.html", "utf8")) });
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form method="POST" action="/uploads" enctype="multipart/form-data">
<input type="file" value="上传" name="flies">
<input type="file" value="上传" name="flies">
<input type="submit">
</form>
</body>
</html>
只需略微修改便可以