使用 express-fileupload 上传文件不起作用
Uploading a file with express-fileupload doesn't work
我正在尝试在 Node js 中使用 express-fileupload (express-fileupload) 上传文件。
但是我还没有成功。
我的app.js(我的快递服务器是运行)看起来像这样:
const express = require('express');
const exphbs = require('express-handlebars');
const fileUpload = require('express-fileupload');
const app = express();
const port = 5000;
app.use(fileUpload({
debug: true
}));
app.use(express.static('./upload'));
const routes = require('./server/routes/task');
app.use('/', routes);
app.listen(port, () => console.log(`Listening on port ${port}`));
我的 task.js(我的路线所在的位置)如下所示:
const express = require('express');
const router = express.Router();
const taskController = require('../controllers/taskController');
router.post('/admin/fileUpload', taskController.fileUpload);
module.exports = router;
我的taskController.js(我的路线受到控制)看起来像这样:
exports.fileUpload = (req, res) => {
let sampleFile;
let uploadPath;
if (!req.files || Object.keys(req.files).length === 0) {
res.render('admin', { error: `No files were uploaded!` });
} else {
sampleFile = req.files.sampleFile;
uploadPath = __dirname + '/upload/' + sampleFile.name;
sampleFile.mv(uploadPath, function (err) {
if (err) throw err;
res.render('admin', { alert: `File ${sampleFile.name} successfully uploaded!` });
}
}
}
我的 admin.hbs(我也在使用 express-handlebars)看起来像这样:
<form method="POST" action="/admin/fileUpload">
<h2>Upload File</h2>
<input type="file" enctype="multipart/form-data" name="sampleFile">
<button type="submit">
</form>
因此,当我启动 express 服务器时,我的日志如下所示:
[nodemon] starting `node app.js`
Listening on port 5000
Express-file-upload: Request is not eligible for file upload!
我花了几个小时试图找到问题,但还没有成功。
所以,我花了一段时间才找到我的问题。
我在表单和上传路径中犯了两个小错误。
<form method="POST" action="/admin/fileUpload">
<h2>Upload File</h2>
<input type="file" enctype="multipart/form-data" name="sampleFile">
<button type="submit">
</form>
错了。我在错误的地方添加了“enctype”。它应该在表单中而不是在输入字段中。
我的表单的解决方案:
<form method="POST" action="/admin/fileUpload" enctype="multipart/form-data">
<h2>Upload File</h2>
<input type="file" name="sampleFile">
<button type="submit">
</form>
我上传路径的解决方法:
而不是
uploadPath = __dirname + '/upload/' + sampleFile.name;
我需要这样做
uploadPath = './upload/' + sampleFile.name;
我正在尝试在 Node js 中使用 express-fileupload (express-fileupload) 上传文件。 但是我还没有成功。
我的app.js(我的快递服务器是运行)看起来像这样:
const express = require('express');
const exphbs = require('express-handlebars');
const fileUpload = require('express-fileupload');
const app = express();
const port = 5000;
app.use(fileUpload({
debug: true
}));
app.use(express.static('./upload'));
const routes = require('./server/routes/task');
app.use('/', routes);
app.listen(port, () => console.log(`Listening on port ${port}`));
我的 task.js(我的路线所在的位置)如下所示:
const express = require('express');
const router = express.Router();
const taskController = require('../controllers/taskController');
router.post('/admin/fileUpload', taskController.fileUpload);
module.exports = router;
我的taskController.js(我的路线受到控制)看起来像这样:
exports.fileUpload = (req, res) => {
let sampleFile;
let uploadPath;
if (!req.files || Object.keys(req.files).length === 0) {
res.render('admin', { error: `No files were uploaded!` });
} else {
sampleFile = req.files.sampleFile;
uploadPath = __dirname + '/upload/' + sampleFile.name;
sampleFile.mv(uploadPath, function (err) {
if (err) throw err;
res.render('admin', { alert: `File ${sampleFile.name} successfully uploaded!` });
}
}
}
我的 admin.hbs(我也在使用 express-handlebars)看起来像这样:
<form method="POST" action="/admin/fileUpload">
<h2>Upload File</h2>
<input type="file" enctype="multipart/form-data" name="sampleFile">
<button type="submit">
</form>
因此,当我启动 express 服务器时,我的日志如下所示:
[nodemon] starting `node app.js`
Listening on port 5000
Express-file-upload: Request is not eligible for file upload!
我花了几个小时试图找到问题,但还没有成功。
所以,我花了一段时间才找到我的问题。
我在表单和上传路径中犯了两个小错误。
<form method="POST" action="/admin/fileUpload">
<h2>Upload File</h2>
<input type="file" enctype="multipart/form-data" name="sampleFile">
<button type="submit">
</form>
错了。我在错误的地方添加了“enctype”。它应该在表单中而不是在输入字段中。
我的表单的解决方案:
<form method="POST" action="/admin/fileUpload" enctype="multipart/form-data">
<h2>Upload File</h2>
<input type="file" name="sampleFile">
<button type="submit">
</form>
我上传路径的解决方法:
而不是
uploadPath = __dirname + '/upload/' + sampleFile.name;
我需要这样做
uploadPath = './upload/' + sampleFile.name;