节点js正常文件上传代码在执行时显示错误
node js normal file upload code shows error while executing
我正在尝试一个简单的 node.js
文件上传代码用于测试目的,使用 express
和 multer
modules.My 代码查找 html
,例如:-
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="http://127.0.0.1:8081/file_upload" method="POST"
enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
和我的 server.js
代码看起来像:-
var express = require('express');
var app = express();
var fs = require("fs");
var bodyParser = require('body-parser');
var multer = require('multer');
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(multer({ dest: '/tmp/'}));
app.get('/index.html', function (req, res) {
res.sendFile( __dirname + "/" + "index.html" );
});
app.post('/file_upload', function (req, res) {
console.log(req.files.file.name);
console.log(req.files.file.path);
console.log(req.files.file.type);
var file = __dirname + "/" + req.files.file.name;
fs.readFile( req.files.file.path, function (err, data) {
fs.writeFile(file, data, function (err) {
if( err ){
console.log( err );
}else{
response = {
message:'File uploaded successfully',
filename:req.files.file.name
};
}
console.log( response );
res.end( JSON.stringify( response ) );
});
});
});
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
});
现在,当我从命令提示符 运行 server.js
时,例如:- node server.js
我的服务器没有启动,它会抛出一些错误,例如图像 :-
SOuřaan Gřg,
您的行 var multer = require('multer');
正在返回对象,而不是中间件函数。您可以使用三个中间件功能。
查看 docs。
In case you need to handle a text-only multipart form, you can use any of the multer methods (.single(), .array(), fields()).
.single(fieldname)
Accept a single file with the name fieldname. The single file will be stored in req.file.
.array(fieldname[, maxCount])
Accept an array of files, all with the
name fieldname. Optionally error out if more than maxCount files are
uploaded. The array of files will be stored in req.files.
.fields(fields)
Accept a mix of files, specified by fields. An object
with arrays of files will be stored in req.files.
你必须改变app.use(multer({ dest: '/tmp/'}));
。您正在传递一个对象。
同样根据文档:
WARNING: Make sure that you always handle the files that a user uploads. Never add multer as a global middleware since a malicious user could upload files to a route that you didn't anticipate. Only use this function on routes where you are handling the uploaded files.
您应该将中间件分配到处理上传的位置。对于您的情况,您可以执行以下操作:
var upload = multer({ dest: '/tmp/' });
app.post('/file_upload', upload.single('file'), function (req, res, next) {
// req.file is the `file` file
// req.body will hold the text fields, if there were any
})
我正在尝试一个简单的 node.js
文件上传代码用于测试目的,使用 express
和 multer
modules.My 代码查找 html
,例如:-
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="http://127.0.0.1:8081/file_upload" method="POST"
enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
和我的 server.js
代码看起来像:-
var express = require('express');
var app = express();
var fs = require("fs");
var bodyParser = require('body-parser');
var multer = require('multer');
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(multer({ dest: '/tmp/'}));
app.get('/index.html', function (req, res) {
res.sendFile( __dirname + "/" + "index.html" );
});
app.post('/file_upload', function (req, res) {
console.log(req.files.file.name);
console.log(req.files.file.path);
console.log(req.files.file.type);
var file = __dirname + "/" + req.files.file.name;
fs.readFile( req.files.file.path, function (err, data) {
fs.writeFile(file, data, function (err) {
if( err ){
console.log( err );
}else{
response = {
message:'File uploaded successfully',
filename:req.files.file.name
};
}
console.log( response );
res.end( JSON.stringify( response ) );
});
});
});
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
});
现在,当我从命令提示符 运行 server.js
时,例如:- node server.js
我的服务器没有启动,它会抛出一些错误,例如图像 :-
SOuřaan Gřg,
您的行 var multer = require('multer');
正在返回对象,而不是中间件函数。您可以使用三个中间件功能。
查看 docs。
In case you need to handle a text-only multipart form, you can use any of the multer methods (.single(), .array(), fields()).
.single(fieldname)
Accept a single file with the name fieldname. The single file will be stored in req.file..array(fieldname[, maxCount])
Accept an array of files, all with the name fieldname. Optionally error out if more than maxCount files are uploaded. The array of files will be stored in req.files..fields(fields)
Accept a mix of files, specified by fields. An object with arrays of files will be stored in req.files.
你必须改变app.use(multer({ dest: '/tmp/'}));
。您正在传递一个对象。
同样根据文档:
WARNING: Make sure that you always handle the files that a user uploads. Never add multer as a global middleware since a malicious user could upload files to a route that you didn't anticipate. Only use this function on routes where you are handling the uploaded files.
您应该将中间件分配到处理上传的位置。对于您的情况,您可以执行以下操作:
var upload = multer({ dest: '/tmp/' });
app.post('/file_upload', upload.single('file'), function (req, res, next) {
// req.file is the `file` file
// req.body will hold the text fields, if there were any
})