HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css">
<title>Node file uploads</title>
</head>
<body>
<div class="container">
<h1>File upload</h1>
<%= typeof msg!= 'undefined' ? msg: ''%>
<form action="/upload" method="POST" enctype="multipart/form-data">
<div class="file-field input-field">
<div class="btn grey">
<span>File</span>
<input name="myImage" type="file">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
</div>
<button type="submit" class="btn">Submit</button>
</form>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
</body>
</html>
NODEJS CODE
const express = require('express');
const multer = require('multer');
const ejs = require('ejs');
const path= require('path');
const bodyParser = require('body-parser');
const app = express();
// middle ware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//set storage engine
const storage = multer.diskStorage({
destination: './public/uploads/',
filename:(res,file,cb)=>{
cb(null,file.fieldname+'-'+Date.now()+path.extname(file.originalname));
}
});
// init app
const upload = multer({storage:storage}).single('myImage');
const port = 3000;
// ejs
app.set('view engine','ejs');
//public folder
app.use(express.static('./public'));
// routing
app.get('/', (req,res)=>{
res.render('index');
});
app.post('/upload',(req,res)=>{
upload(res,res,(err)=>{
if(err){
res.render('index',{msg:err});
res.send('error uploading');
}else{
// console.log(req.file);
res.send('test');
}
});
});
app.listen(port,()=>{
console.log('server is connected at '+ port);
});
错误
TypeError:无法读取属性&#39; transfer-encoding&#39;未定义的 在hasbody(/var/www/html/nodeuploads/node_modules/type-is/index.js:93:21) 在typeofrequest(/var/www/html/nodeuploads/node_modules/type-is/index.js:127:8) 在multerMiddleware(/var/www/html/nodeuploads/node_modules/multer/lib/make-middleware.js:18:10) 在app.post(/var/www/html/nodeuploads/app.js:40:4) 在Layer.handle [as handle_request](/var/www/html/nodeuploads/node_modules/express/lib/router/layer.js:95:5) 在下一个(/var/www/html/nodeuploads/node_modules/express/lib/router/route.js:137:13) 在Route.dispatch(/var/www/html/nodeuploads/node_modules/express/lib/router/route.js:112:3) 在Layer.handle [as handle_request](/var/www/html/nodeuploads/node_modules/express/lib/router/layer.js:95:5) at /var/www/html/nodeuploads/node_modules/express/lib/router/index.js:281:22 在Function.process_params(/var/www/html/nodeuploads/node_modules/express/lib/router/index.js:335:12)
当我点击下面的提交按钮时,会显示消息
传输编码&#39;未定义的
如果需要其他详细信息来调试此问题,请与我们联系
由于