我有一个简单的html上传表单
<h1>Upload File Here</h1>
<form methods="post" enctype="multipart/form-data" action="/">
<input type="file" name="filename" /> <input type="submit" value="upload" />
</form>
我希望上传的文件打到“ / upload”文件夹。我正在使用以下表达代码来做到这一点。
const express = require("express");
const app = express();
upload = require("express-fileupload");
app.listen(80);
app.use(upload());
console.log("Server Started");
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
});
app.post("/", (req, res) => {
if (req.files) {
var file = req.files.filename,
filename = file.name;
file.mv("/upload/" + filename, err => {
if (err) {
console.log(err);
res.send("error occured");
} else {
res.send("done!");
}
});
}
});
启动服务器并在Chrome中转到localhost
时,我看到了上传表单。如果我尝试上传内容,URL框将更改以反映我尝试上传的文件,但该文件不会出现在“ / upload”文件夹中。关于我正在犯什么明显错误的任何想法?
谢谢!
解决方案是我的表格。我需要设置action="http://localhost:80/upload"
新的HTML是
<h1>Upload File Here</h1>
<form
ref="uploadForm"
id="uploadForm"
action="http://localhost:80/upload"
method="post"
enctype="multipart/form-data"
>
<input type="file" name="filename" /> <input type="submit" value="upload" />
</form>
答案 0 :(得分:0)
解决方案是我的表格。我需要设置
action="http://localhost:80/upload"
新的HTML是
<h1>Upload File Here</h1>
<form
ref="uploadForm"
id="uploadForm"
action="http://localhost:80/upload"
method="post"
enctype="multipart/form-data"
>
<input type="file" name="filename" /> <input type="submit" value="upload" />
</form>