CNode

fs:貌似renameSync()文件时出错了

SSoftor发布于14 年前最后回复11 年前21 回复23579 浏览0 收藏
查看回复

回复 (21)

F
fish#1·14 年前

fs.rename 之前遇到过类似的问题,临时文件写成功了,但是就是无法移动... 猜测是由于磁盘分区导致的,你的例子中也符合这个条件 C: -> E:

改变formidable的默认零时文件夹路径,保证和目标目录处于同一个磁盘分区 formidable.IncomingForm.UPLOAD_DIR = your_path

这样应该ok的

S
Softor#2·14 年前
引用 fishfs.rename 之前遇到过类似的问题,临时文件写成功了,但是就是无法移动... 猜测是由于磁盘分区导致的,你的例子中也符合这个条件 C: E: 改变formidable的默认零时文件夹路径...
function upload(response, request) {
  console.log("Request handler 'upload' was called.");
  var form = new formidable.IncomingForm();
  form.uploadDir = "tmp"
 // console.log("about to parse");
  form.parse(request, function(error, fields, files) {
    //console.log("parsing done");
	console.log("F.U.P: " + files.upload.path);
	setTimeout(function(){
	try{
	    fs.renameSync(files.upload.path, "tmp/test.png");
	}catch(e){
		console.log(e);
	}
    response.writeHead(200, {"Content-Type": "text/html"});
    response.write("received image:<br/>");
    response.write("<img src='/show' />");
    response.end();
	},1000
	);
  });
}

搞定了。谢谢你~

another reply: https://groups.google.com/group/nodejs/browse_thread/thread/222cb0fd30906d4e

S
sun_dsk1#3·14 年前

我也碰到了这个问题,windows上会有。按照fish的方法解决了,只要在工程目录下建个目录A,然后把formidable的uploadDir指向这个目录(相对路径即可),就能使用这个目录了。

F
fish#4·14 年前

github上提了个issue,有人解释了。 https://github.com/joyent/node/issues/2703#issuecomment-3824789

A
aaroncanter#5·14 年前

我也遇到了这个问题。多谢指点,开始开到错误提示“文件或者目录不存在”,而且打印出的路径是指向C盘,将感到奇怪。现在搞定了,3Q。

L
ljwking#6·14 年前

将fs.renameSync(files.upload.path, "/tmp/test.png");中的"/tmp/test.png"改为 "./tmp/test.png"也是可以的。我就是这么改的。

L
leizongmin#7·14 年前

在Windows上尽量不要操作C盘,权限问题很麻烦的

H
hkant#8·14 年前
引用 ljwking将fs.renameSync(files.upload.path, "/tmp/test.png");中的"/tmp/test.png"改为 "./tmp/test.png"也是可以的。我就是...

我在winxp+node0.6下测试,你的这个方法出错,错误提示与楼主的类似

Z
zsytssk#9·14 年前

fs.rename()不能跨磁盘移动文件 renameSync 不能创建新目录 参考 http://stackoverflow.com/questions/10084000/why-use-nodejs-upload-exception

N
nodejsswb#10·14 年前
/home/hd-user/github/nodejs-beginner/requestHandlers.js:61
    console.log(files.upload.path);
                            ^
TypeError: Cannot read property 'path' of undefined

这个错误大概什么原因呢?

S
sumaolin#12·13 年前
引用 nodejsswb这个错误大概什么原因呢?

你的例子中的upload()是不是该过啊,files.upload.path 中的path 应该和请求路径的pathname 一致的

X
xmlovecss#14·13 年前

我也照抄的,这里的问题改了下,手动创建了tmp文件。 代码如下:

// 响应upload
var upload = function(response, request) {
    console.log('Request handler "upload" was called.');
    var form=new formidable.IncomingForm();
    console.log('about to parse');

    // 写一个临时路径
    form.uploadDir='tmp';
    form.parse(request,function(err,fields,files){
        console.log('parsing done');
        console.log(files.upload);
        // 同步操作文件,需要try catch
        try{
            fs.renameSync(files.upload.path,'tmp/test.png');
        }catch(e){
            console.log(e);
        }
        
        response.writeHead(200,{'Content-Type':'text/html'});
        response.write('received img:</br>');
        response.write('<a href="/show"><img src="/show" /></a>');
        response.end();
    });
};
// 显示文件
var show=function(response){
    console.log('request handler "upload" was called');
    fs.readFile('./tmp/test.png','binary',function(err,file){
        if(err){
            response.writeHead(500,{'Content-Type':'text/plain'});
            response.write(err+'\n');
            response.end();
        }else{
            response.writeHead(200,{'Content-Type':'image/png'});
            response.write(file,'binary');
            response.end();
        }
    });
};
X
xmlovecss#15·13 年前

upload接口,更改了输出的html结构:response.write(''); 不然在windows图片无法显示。 show接口,fs.readFile('./tmp/test.png')多了一个'./',我还不知道这个是啥意思。 若不加的话,报错信息提示,位置是在:f:\tmp\test.png。估计加了'./'就在当前目录下寻找文件了。

T
taballa#16·13 年前

/* Possible error on Windows systems: tried to rename to an already existing file */ fs.rename(files.upload.path, "/tmp/test.png", function(err) { if (err) { fs.unlink("/tmp/test.png"); fs.rename(files.upload.path, "/tmp/test.png"); } });

X
xiaosheng2014#17·12 年前

我也出现同样的的错误,已经按照大家的办法解决了,谢谢

P
pyramid1988#18·11 年前

我试了一下,其实不需要这一句 form.uploadDir = "tmp" 在绝大数系统中“/tmp/test.png”中的第一个“/”指的是系统根目录,具体见下面的链接 链接 觉得应该是windows在node中不识别“/”所致,@xmlovecss的问题也是这种情况。

F
fish#19·14 年前

ENOENT 文件不存在 ,看看formidable临时文件写成功没?

另外文件同步操作得try{}catch(e){},要不然异常会导致进程直接退出

S
Softor#20·14 年前
引用 fishENOENT 文件不存在 ,看看formidable临时文件写成功没? 另外文件同步操作得try{}catch(e){},要不然异常会导致进程直接退出

我看到临时文件夹里有上传的文件。 我参照的例子是: http://nodebeginner.org/index-zh-cn.html 上文贴出的是其中的最后一段代码。

请指教~

G
gxmari007#21·14 年前

fs.renameSync这个方法没有问题的,应该是你其他地方写错了

参与回复
登录后即可参与回复。登录