1

代码

form.parse(request, function(error, fields, files) {
    console.log('parsing done');

    fs.renameSync(files.upload.path, 'c:/tmp/test.png');
    response.writeHead(200, {'Content-Type': 'text/html'});
    response.write('received image:<br/>');
    response.write('<img src="/show" />');
    response.end();
});


异常信息

返回 binding.rename(pathModule._makeLong(oldPath)),


     错误:ENOENT,在 Object.renameSync(fs.js:330:18) 处没有这样的文件或目录 'C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\esdafdace32323432a3f'

4

1 回答 1

4

这是因为fs.rename()不能在两个不同的磁盘之间移动文件
其他renameSync不会创建新文件夹,所以你应该确保tmp/它必须存在。您需要在源代码文件夹中创建 tmp 文件夹,然后设置uploadDir 如下:

var form = new formidable.IncomingForm();
  form.uploadDir = "tmp";
  console.log("about to parse");
  form.parse(request, function(error, fields, files) {
    console.log(files.upload.path);
    try{
        fs.renameSync(files.upload.path, "tmp/test.jpg");
    }catch(e){
        console.log(e);
    }
    response.writeHead(200, {"Content-Type": "text/html"});
    response.write("received image:<br/>");
    response.write("<img src='/show' />");
    response.end();
  });
于 2012-05-29T06:06:00.757 回答