Код: Выделить всё
'use strict';
const path = require('path');
const express = require('express');
const app = express();
// Set up the view engine
app.set('view engine', 'pug');
app.set('views', path.join(__dirname, 'views'));
const UPLOAD = "/upload";
app.get(UPLOAD, async (req, res) => {
return res.render("upload");
});
const fileUpload = require('express-fileupload');
app.post(UPLOAD, fileUpload(), async (req, res) => {
let keys = Object.keys(req.files);
fs.readFile(req.files[keys[0]].path, (err, e) => {
if(err) console.log(err);
fs.writeFile(keys[0], e, (err) => {
console.log(err)
})
})
req.flash("success", "File uploaded");
return res.redirect(UPLOAD);
});
const http = require('http');
const httpServer = http.createServer(app);
let server = httpServer.listen(8080, () => {
console.log("\n# HTTP server started");
});
< /code>
и представление: < /p>
doctype html
html
body
form.left(method='POST')
input(type='file', id="file", name="filename", accept=".xls,.xlsx,.txt")
input(type="submit", name="submit", value="Upload")
Код: Выделить всё
mre.js:24
let keys = Object.keys(req.files);
TypeError: Cannot convert undefined or null to object
Как я могу загрузить файл на сервер Nodejs?
Подробнее здесь: https://stackoverflow.com/questions/796 ... file-field
Мобильная версия