exception - 为什么使用nodejs上传异常

标签 exception node.js

代码

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();
});


异常信息

返回绑定(bind).rename(pathModule._makeLong(oldPath)),

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

最佳答案

这是因为fs.rename() can't move file between two different disks
其他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();
  });

关于exception - 为什么使用nodejs上传异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10084000/

相关文章:

c# - .NETCore 应用程序因 StackOverflowException 而终止

javascript - 如何在没有 express 的情况下处理多部分/表单数据请求

node.js - 如何在 Node redis客户端上设置读取超时?

c++ - 如何避免将 std::string 放入异常类中?

java - 如何修复 java.net.SocketException : Broken pipe?

javascript - 在 Node 中抓取 .aspx 页面

node.js - 在 Express 中将菜单呈现为 Jade View 部分

node.js - 无法运行全局安装的 Node 模块

java - 捕获多个异常的处理程序?

Java EE/Struts 异常处理