我有一个非常简单的节点脚本,应该只显示一个静态网页与上传表单,并写入任何上传的文件到磁盘。
const formidable = require('formidable')
const http = require('http')
var fs = require('fs');
const app = express()
const serve = http.Server(app)
const PORT = process.env.PORT || '5000'
// Display upload page at root
app.use(express.static('../client'))
//Start HTTP with express
serve.listen(PORT, () => {
console.log(`Listening on ${PORT}`)
})
app.post('/submit-form', function (req, res){
var form = new formidable.IncomingForm();
form.on('fileBegin', function (name, file){
file.path = __dirname + '/uploads/' + file.name;
});
form.parse(req);
form.on('file', function (name, file){
console.log('Uploaded ' + file.name);
});
});
app.listen()页面显示正常,并按预期发送了一个post请求,但在上传后收到以下错误:
Error: ENOENT: no such file or directory, open ' the path where the upload should be '
Emitted 'error' event at:
at lazyFs.open (internal/fs/streams.js:273:12)
at FSReqWrap.oncomplete (fs.js:141:20)我找不到任何有类似错误的人,但我假设我可能正在做一些愚蠢的事情。
有人能帮我吗?
发布于 2020-09-05 03:31:29
在监听事件之前,您必须先解析请求。试试这个:
app.post('/submit-form', function (req, res){
var form = new formidable.IncomingForm();
form.parse(req)
.on('fileBegin', function (name, file){
file.path = __dirname + '/uploads/' + file.name;
})
.on('file', function (name, file){
console.log('Uploaded ' + file.name);
});
});https://stackoverflow.com/questions/61264473
复制相似问题