Express-fileupload req.files 空
Express-fileupload req.files empty
这是我的 app.js
const express = require('express');
const upload = require("express-fileupload");
const app = express();
app.use(upload());
app.get("/", function(req, res){
res.sendFile(__dirname + "/index.html");
})
app.post("/", function(req, res){
if(req.files){
console.log(req.files);
}else{
console.log("error");
}
})
app.listen(3000, function(){
console.log("Success");
})
这是 index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>File upload in node js</h1>
<form action="/" method="POST">
<input type="file" name="file">
<button type="submit" value="upload">upload</button>
</form>
</body>
</html>
我从控制台收到“错误”,这意味着 req.files 是空的或我猜不存在
我不知道如何解决这个问题,我今天开始使用 fileupload,所以我对此不太熟悉
请帮忙
提交文件时,必须使用 multipart / formdata 标签。 see
<form action="/" method="POST" enctype='multipart/form-data'>
<input type="file" name="file">
<button type="submit" value="upload">upload</button>
</form>
此外,当使用 javascript 发出 post 请求时,您必须将其作为新的 FormData () 发送。 see
这是我的 app.js
const express = require('express');
const upload = require("express-fileupload");
const app = express();
app.use(upload());
app.get("/", function(req, res){
res.sendFile(__dirname + "/index.html");
})
app.post("/", function(req, res){
if(req.files){
console.log(req.files);
}else{
console.log("error");
}
})
app.listen(3000, function(){
console.log("Success");
})
这是 index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>File upload in node js</h1>
<form action="/" method="POST">
<input type="file" name="file">
<button type="submit" value="upload">upload</button>
</form>
</body>
</html>
我从控制台收到“错误”,这意味着 req.files 是空的或我猜不存在 我不知道如何解决这个问题,我今天开始使用 fileupload,所以我对此不太熟悉 请帮忙
提交文件时,必须使用 multipart / formdata 标签。 see
<form action="/" method="POST" enctype='multipart/form-data'>
<input type="file" name="file">
<button type="submit" value="upload">upload</button>
</form>
此外,当使用 javascript 发出 post 请求时,您必须将其作为新的 FormData () 发送。 see