【发布时间】:2019-04-15 12:31:03
【问题描述】:
我创建了示例应用程序,使用 express js 将文件上传到本地路径。
app.js
const express = require("express");
const app = express();
const http = require("http").Server(app).listen(3000);
const upload = require("express-fileupload");
app.use(upload());
console.log("Server Started");
app.get("/", function (req, res) {
res.sendFile(__dirname + "/index.html");
}
)
app.post("/", function (req, res) {
if (req.files) {
//console.log(req.files);
const file = req.files.filename;
const filename = file.name;
file.mv("./upload/" + filename, function (err) {
if (err) {
console.log(err);
res.send("error occured");
}
else {
res.send("Done");
}
})
}
})
index.html
<div>
<h1 style="align-content: center">Upload your file here!</h1>
</div>
<div style=" background-color: white;
padding:64px;
display:flex;
align-items:flex-start;
justify-content: flex-start;
box-shadow: 0 15px 30px 0 rgba(0, 0, 0, 0.11), 0 20px 15px 0 rgba(0, 0, 0, 0.08);
box-sizing:border-box">
<form label="upload" method="post" enctype="multipart/form-data" action="/">
<label> Enter reference</label>
<input type="text"></input>
<br><br>
<input type="file" name="filename">
<input type="submit" value="upload">
</form>
</div>
我需要帮助才能访问 app.js 文件中输入 type = "text" 中输入的文本内容。任何帮助将不胜感激。
【问题讨论】:
-
NodeJS 是服务器端的,这意味着没有像客户端 javascript 那样可以访问的 html。如果您想访问存储在 dom 中的数据,您必须将其从客户端发送到服务器。
-
@CodeSpirit — 这就是 HTML 中的
<form>所做的。 -
@Quentin - 就我而言,请告诉我如何访问 app.js 上的 type = "text"。