Node.js EXDEV重命名错误 [英] Node.js EXDEV rename error

查看:93
本文介绍了Node.js EXDEV重命名错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩一本我在一本关于Node.js的书中找到的代码。这是一个上传图片的简单应用程序。

I've been playing with some code I found in a book about Node.js. It is a simple app which uploads images.

它显示EXDEV错误(500错误:EXDEV,重命名)。

It shows the EXDEV error (500 Error: EXDEV, rename).

有人能给我一个暗示吗?这是我的代码:

Could someone give me a hint? Here's my code:

exports.submit = function(dir) {
    return function(req, res, next) {
        var img = req.files.photo.image;
        var name = req.body.photo.name || img.name;
        var path = join(dir, img.name);

        fs.rename(img.path, path, function (err) {
            if(err) return next(err);

            Photo.create({
                name: name,
                path: img.name
            }, function (err) {
                if(err) return next(err);
                res.redirect('/');
            });
        });
    };
};


推荐答案

重命名文件无法跨设备完成。我的猜测是你的上传目录(默认为 / tmp )在另一个分区/驱动器上作为你的目标目录(包含在 dir 变量)。

Renaming files cannot be done cross-device. My guess is that your upload directory (which by default is /tmp) is on another partition/drive as your target directory (contained in the dir variable).

一些解决方案:


  • configure上传目录与目标目录位于同一分区/驱动器上;这取决于你用来处理文件上传的模块, express.bodyParser (以及它使用的模块, connect.multipart )接受 uploadDir 选项即可使用;

  • 在启动Node应用程序之前,将 TMPDIR 环境变量设置为指向同一分区上的临时目录/ drive作为目标目录。如果您使用的是Unix类型的操作系统:

  • configure the upload directory to be on the same partition/drive as your target directory; this depends on which module you're using to handle file uploads, express.bodyParser (and the module it uses, connect.multipart) accepts an uploadDir option that you can use;
  • before starting your Node app, set the TMPDIR environment variable to point to a temporary directory on the same partition/drive as your target directory. If you're using a Unix-type OS:

env TMPDIR=/path/to/directory node app.js


  • 而不是从shell设置环境变量,将其设置在Node应用程序的顶部:

  • instead of setting the environment variable from your shell, set it at the top of your Node app:

    process.env.TMPDIR = '/path/to/directory';
    


  • 而不是重命名,请使用像 mv 可以跨设备工作;

  • instead of renaming, use a module like mv that can work cross-device;
  • 这篇关于Node.js EXDEV重命名错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆