在windows环境下使用renameSync错误,实在没辙,求高手帮忙
发布于 12 年前 作者 arrowing 9499 次浏览 最后一次编辑是 8 年前

跟着这里的教程走到最后部分:http://www.nodebeginner.org/index-zh-cn.html

附上我的上传函数代码:

function upload(response, request){
var form = new formidable.IncomingForm();
//form.uploadDir = "tmp";
form.parse(request, function(error, fields, files) {
	//var filename = path.basename(files.upload.path);
	fs.renameSync(files.upload.path, "/tmp/test.png");
	response.writeHead(200, {"Content-Type": "text/html"});
	console.log(files.upload.path);
	response.write("<img src='/show' />");
	response.end();
 });}

在网上找了许多方法,都无法实现,大部分说的是,rename及其相关方法,无法移动文件到别的区间。解决方法都是设置uploadDir,我设置了无效呢。

我发现这里的files.upload.path很奇怪,我取到文件名,然后

fs.renameSync('/tmp' + filename, "/tmp/test.png");

结果提示居然和没改的一样,好像传递给renameSync的第一个参数无效似的 附错误提示:请高手支招

fs.js:439 return binding.rename(pathModule._makeLong(oldPath), ^ Error: ENOENT, no such file or directory 'F:\cygwin\tmp\befd1e150d5d8451478fcef24f0d657b’ at Object.fs.renameSync (fs.js:439:18) at F:\cygwin\home\Arrowing\requestHandlers.js:61:6 at IncomingForm.parse (F:\cygwin\home\Arrowing\node_modules\formidable\lib\incoming_form.js:121:9) at IncomingForm.EventEmitter.emit (events.js:85:17) at IncomingForm._maybeEnd (F:\cygwin\home\Arrowing\node_modules\formidable\lib\incoming_form.js:383:8) at IncomingForm.handlePart (F:\cygwin\home\Arrowing\node_modules\formidable\lib\incoming_form.js:212:12) at File.end (F:\cygwin\home\Arrowing\node_modules\formidable\lib\file.js:71:5) at WriteStream.flush (fs.js:1509:9) at Object.oncomplete (fs.js:297:15) at process.startup.processMakeCallback.process._makeCallback (node.js:238:20)

14 回复

没人哦~自己解决了,附解决代码

function upload(response, request){
var form = new formidable.IncomingForm();
form.uploadDir = "upload"; // project dir
form.parse(request, function(error, fields, files) {
	fs.renameSync(files.upload.path, "./" + form.uploadDir + "/test.png");
	response.writeHead(200, {"Content-Type": "text/html"});
	console.log(files.upload.path);
	response.write("<img src='/show' />");
	response.end();
 });}

搜到楼主的帖子,估计楼主在百度贴吧分享了吧。刚刚试了,form.uploadDIr = “upload”;再把”/tmp/test.png“改成"tmp/test.png".

form.parse(request, function(error, fields, files) {
  // 这里要先判断一下error,否则后面有可能产生“莫名其妙”的错误
  if (error) throw error;
  // ...
});

@leizongmin 谢谢,不知道为什么,之前做了OK,最近几天重新测试,request的data事件一直在接收数据,end了之后,form.parse一直没执行回调函数。只好把data事件去掉才行,你知道是什么问题吗?

嗯,多多交流

@arrowing 没看见代码,猜不出来神马原因。

@leizongmin

var http = require(‘http’), url = require(‘url’); function start(route, handle){ function onRequest(request, response){ var url_obj = url.parse(request.url); var pathname = url_obj.pathname, getData = url_obj.query, postData = ‘’;

	/*
	request.setEncoding("utf8");
	request.addListener('data', function(postDataChunk){
		postData += postDataChunk;
	});

	request.addListener('end', function(){
		route(handle, pathname, response, request, postData, getData);
	});
	*/
	route(handle, pathname, response, request, postData, getData);
}
http.createServer(onRequest).listen(80);
console.log('Server has started.');

} exports.start = start;

这个是服务器入口代码,就是添加了注释了那块代码就有问题 如果那里在控制台输入循环取得的数据,控制台就会假死,一直获取不完数据一样 这样: request.addListener(‘data’, function(postDataChunk){ postData += postDataChunk; console.log(postData); });

然后上传那边解析请求的时候,也一直未完成,没能触发回调函数 form.parse(request, function(error, fields, files) { //alert(‘没进来’); });} 第一次用不会这样的,最近用就会了,不知道是什么原因

@leizongmin 不好意思啊,这代码格式,换行太蛋疼了,用得不好~~费你眼神了。。。

@arrowing 去掉这句request.setEncoding("utf8");就可以了。 你这个例子是从这里抄的吧?http://www.nodebeginner.org/index-zh-cn.html 真是坑人不浅

@leizongmin 不行哦,本来就没这句的,我以为要编码错了才加的,没想到也不行。按道理这个处理编码应该没问题才对

@leizongmin 虽然这个问题和这个设置编码无关,不过这个设置编码确实有问题。加了之后,会出现连接被重置的Aborted状态

@arrowing 没有完整代码,一下子看不出神马问题来。你自己调试吧,可以直接改本地的formidable模块的代码,看看都哪里才开始没反应的

在一个盘符下用

回到顶部