Nodejs入门遇到的问题,formidable上传图片时报错
发布于 11 年前 作者 2eron 6445 次浏览 最后一次编辑是 8 年前

基本上是根据Nodejs入门一步步做的,代码也基本一样: index.js:

var server = require('./server.js');
var router = require('./router.js');
var requestHandlers = require('./requestHandlers.js');

var handle = {};
handle['/'] = requestHandlers.start;
handle['/start'] = requestHandlers.start;
handle['/upload'] = requestHandlers.upload;
handle['/show'] = requestHandlers.show;

server.start(router.route,handle);

server.js:

var http = require("http");
var url = require('url');
exports.start = function(route, handle){
	http.createServer(function(request, response){
		var pathname = url.parse(request.url).pathname;
		route(handle, pathname, response, request);
	}).listen(3000);
	console.log('Server has started!');
};

router.js:

function route (handle, pathname, response, request) {
	console.log('A request for' + pathname);

	if(typeof handle[pathname] === 'function'){
		handle[pathname](response, request);
	}else{
		console.log('No request handler for ' + pathname);

		response.writeHead(404, {'Content-Type': 'text/plain'});
		response.write('404 not found');
		response.end();
	}
}
exports.route = route;

requestHandler.js:

var querystring = require('querystring'),
	fs = require('fs'),
	formidable = require('formidable'),
	util = require('util');

function start (response) {
	console.log('Request handler "start" was called.');

	var body = '<html>'+
			    '<head>'+
			    '<meta http-equiv="Content-Type" content="text/html; '+
			    'charset=UTF-8" />'+
			    '</head>'+
			    '<body>'+
			    '<form action="/upload" method="post" enctype="multipart/form-data">'+
			    '<input type="file" name="upload" multiple="multiple">'+
    			'<input type="submit" value="Upload file" />'+
			    '</form>'+
			    '</body>'+
			    '</html>';
	response.writeHead(200, {'Content-Type': 'text/html'});
	response.write(body);
	response.end();
}
function upload (response, request) {
	console.log('Request handler "upload" was called.');

	var form = new formidable.IncomingForm();
	form.uploadDir = 'tmp';
	form.parse(request, function(error, fields, files){
		//fs.renameSync(files.upload.path, '/tmp/test.png');
		try{
			console.log(files.upload.path);
		}catch(e){
			console.log(e);
		}
		response.writeHead(200, {'Content-Type': 'text/html'});
		response.write('Received image: <br>');
		response.write('<img src="/show" />');
		response.end();
	});
	
}
function show (response) {
	console.log('Request handler "show" was called.');
	fs.readFile('/tmp/test.png', 'binary', function(error, file) {
		if(error){
			response.writeHead(500, {'Content-Type': 'text/plain'});
			response.write(error + '\n');
			response.end();
		}else{
			response.writeHead(200, {'Content-Type': 'image/png'});
			response.write(file, 'binary');
			response.end();
		}
	});
}
exports.start = start;
exports.upload = upload;
exports.show = show;

###报错信息: Cannot read property ‘path’ of undefined

####打印files的结果:

{ upload:
   { domain: null,
     _events: {},
     _maxListeners: 10,
     size: 2722,
     path: 'tmp\\2d3ead58151f69465f4fec03b2e27b34',
     name: 'image-holder.png',
     type: 'image/png',
     hash: null,
     lastModifiedDate: Tue May 06 2014 16:44:50 GMT+0800 (中国标准时间),
     _writeStream:
      { _writableState: [Object],
        writable: true,
        domain: null,
        _events: {},
        _maxListeners: 10,
        path: 'tmp\\2d3ead58151f69465f4fec03b2e27b34',
        fd: null,
        flags: 'w',
        mode: 438,
        start: undefined,
        pos: undefined,
        bytesWritten: 2722,
        closed: true } } }

之前看到有人提过这个问题 链接 ,是因为自己设置了encoding,但是我没设置也是这个问题,各位大神帮忙看一下到底哪里有问题啊

10 回复

console.log(files)是可以读出的

@lonso 也是 ‘undefined’

form.uploadDir = ‘tmp’;

可能是这个问题,路径错误。

form.uploadDir = “/my/dir”;这个是formidable的例子

应该不是,这个是我后来加的,开始是默认。而且我看tmp目录里已经写入临时文件

相应的目录下面要建立相应的文件夹,如果没有的话,node不会自动建立,而是报错

文件夹已经创建了

问题解决了么……

入门就学框架? 没意思。我觉得正式学一个框架之前起码最好自己可以实现简单的框架

回到顶部