使用node-formidable上传图片错误
upload image error using node-formidable
我在学习"The Node beginner book",在做书上的练习,练习是呈现一张用户上传的图片。这是一个用node-formidable
写的例子,代码如下:
var formidable = require('formidable'),
http = require('http'),
util = require('util');
http.createServer(function(req, res) {
if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
// parse a file upload
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
return;
}
// show a file upload form
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/upload" enctype="multipart/form-data" '+
'method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload" multiple="multiple"><br>'+
'<input type="submit" value="Upload">'+
'</form>'
);
}).listen(8888);
我 运行 它与 node filename.js
,然后我打开位于 http://localhost:8888/upload 的浏览器,出现如下内容:
我输入一个名字,选择一个文件,结果如下:
我点击upload
按钮,响应如下:
received upload:
{ fields: { title: 'Hello Wolrd' },
files:
{ upload:
File {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
size: 37417,
path: '/tmp/upload_a306115e1e630a0c548b6d820fe803cb',
name: 'myfile_icon_file_4.png',
type: 'image/png',
hash: null,
lastModifiedDate: 2016-10-11T03:52:41.052Z,
_writeStream: [Object] } } }
如何获得属性path
?为什么要在那里创建一个词 File
?
属性 path
在 File
对象上可用,它在其代码中定义。正如您编写的示例,您的 form.parse
函数为您提供了一个名为 files
的 File
对象的映射(见图)。因此,如果您想为刚刚上传的文件获取 path
值,您可以执行以下操作(使用键 upload
因为那是您的 html input
' s name
是):
var pathToFile = files['upload'].path;
请记住,按照您编写示例的方式,这只会在服务器端可用,因此要获得这样的路径,您需要将该行代码放入 form.parse
函数中。
您在示例中给出的打印输出来自响应,这是客户得到的。您用来格式化响应的 util.inspect
明确地将 fields
和 files
对象转换为字符串表示形式以达到 debugging/inspection 的目的,因此这就是您无法访问它们的原因作为客户端的变量。如果您使用我在上面写的行或建议的行概念,它会访问 path
只要它在您的 form.parse
函数内,其中 files
存在于范围内。
我在学习"The Node beginner book",在做书上的练习,练习是呈现一张用户上传的图片。这是一个用node-formidable
写的例子,代码如下:
var formidable = require('formidable'),
http = require('http'),
util = require('util');
http.createServer(function(req, res) {
if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
// parse a file upload
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
return;
}
// show a file upload form
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/upload" enctype="multipart/form-data" '+
'method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload" multiple="multiple"><br>'+
'<input type="submit" value="Upload">'+
'</form>'
);
}).listen(8888);
我 运行 它与 node filename.js
,然后我打开位于 http://localhost:8888/upload 的浏览器,出现如下内容:
我输入一个名字,选择一个文件,结果如下:
我点击upload
按钮,响应如下:
received upload:
{ fields: { title: 'Hello Wolrd' },
files:
{ upload:
File {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
size: 37417,
path: '/tmp/upload_a306115e1e630a0c548b6d820fe803cb',
name: 'myfile_icon_file_4.png',
type: 'image/png',
hash: null,
lastModifiedDate: 2016-10-11T03:52:41.052Z,
_writeStream: [Object] } } }
如何获得属性path
?为什么要在那里创建一个词 File
?
属性 path
在 File
对象上可用,它在其代码中定义。正如您编写的示例,您的 form.parse
函数为您提供了一个名为 files
的 File
对象的映射(见图)。因此,如果您想为刚刚上传的文件获取 path
值,您可以执行以下操作(使用键 upload
因为那是您的 html input
' s name
是):
var pathToFile = files['upload'].path;
请记住,按照您编写示例的方式,这只会在服务器端可用,因此要获得这样的路径,您需要将该行代码放入 form.parse
函数中。
您在示例中给出的打印输出来自响应,这是客户得到的。您用来格式化响应的 util.inspect
明确地将 fields
和 files
对象转换为字符串表示形式以达到 debugging/inspection 的目的,因此这就是您无法访问它们的原因作为客户端的变量。如果您使用我在上面写的行或建议的行概念,它会访问 path
只要它在您的 form.parse
函数内,其中 files
存在于范围内。