Express实现图片上传

首先在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>

只需略微修改便可以

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值