文件上传:如果它不存在,创建目录 [英] File upload: create directory if it doesnt exist

查看:205
本文介绍了文件上传:如果它不存在,创建目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm处理文件上传与的NodeJS强大。这对我的作品。现在我想在结构上载多一点。 I'm传递一个字段从角用我上传的,这是一个 PROJECT_ID 。我要创造我的上传,通过这个ID命名的文件夹,并把它写的文件。

,所以我检查,如果该目录存在如果不是我用 fs.mkdir 创建它,然后将文件写入。尝试此,我得到一个 EINVAL,重命名错误和HTTP 500状态code。

这是我的尝试,任何人有一个想法如何解决这一问题?

  app.post('/上传/'功能(REQ,资源,下一个){
        VAR形式=新formidable.IncomingForm();
        form.keepExtensions = TRUE;
        form.parse(REQ,功能(呃,字段,文件){
            如果(ERR)下一个(ERR);
            fs.exists('上传/+ fields.project_id +'/',函数(存在){
                如果(存在){
                    fs.rename(files.upload.path,上传/+ fields.project_id +/+ files.upload.name,功能(错误){
                        如果(ERR)下一个(ERR);
                        res.render('profile.ejs',{
                            用户:req.user
                        });
                    });
                }其他{
                    fs.mkdir('上传/+ fields.project_id +'/',函数(ERR){
                        如果(ERR)下一个(ERR);
                    });
                    fs.rename(files.upload.path,上传/+ fields.project_id +/+ files.upload.name,功能(错误){
                        如果(ERR)下一个(ERR);
                        res.render('profile.ejs',{
                            用户:req.user
                        });
                    });
                }
            });
        });
    });


解决方案

您正试图创建的目录之前,重命名文件。此外,使用 fs.exists 是不可取的,并且功能将在未来pcated德$ p $

我做你的code一些变化,你可以使用路径模块来创建路径。另外,尽量不管创建目录,如果它已经存在。如果它存在,忽略错误code EEXIST。

更新code:

  //这个添加到beggining
VAR路径=要求('路径');app.post('/上传',函数(REQ,资源,下一个){
    VAR形式=新formidable.IncomingForm();
    form.keepExtensions = TRUE;
    form.parse(REQ,功能(呃,字段,文件){
        如果(ERR)下一个(ERR);
        fs.mkdir(path.resolve('上传',fields.project_id),功能(ERR){
            如果(ERR和放大器;&安培;犯错=='EEXIST'!){
                下一个(ERR);
            }其他{
                fs.rename(files.upload.path,path.resolve('上传',fields.project_id,files.upload.name),功能(ERR){
                    如果(ERR)下一个(ERR);
                    res.render('profile.ejs',{
                        用户:req.user
                    });
                });
            }
        });
    });
});

I´m handling file uploads in NodeJS with formidable. This works for me. Now I want to structure the uploads a little more. I´m passing a field from angular with my uploads, which is a project_id. I want to create a folder in my uploads, named by this ID and write the files in it.

So I check if the directory exists, if not I create it with fs.mkdir and then write the files to it. Trying this, I get a EINVAL, rename error and a HTTP 500 status code.

This is my attempt, anybody got an idea how to fix this?

 app.post('/uploads/', function(req, res, next){
        var form = new formidable.IncomingForm();
        form.keepExtensions = true;
        form.parse(req, function(err, fields, files){
            if (err) next (err);
            fs.exists('uploads/' + fields.project_id + '/', function (exists){
                if (exists) {
                    fs.rename(files.upload.path, 'uploads/' + fields.project_id + '/' +files.upload.name, function(err){
                        if (err) next (err);
                        res.render('profile.ejs',{
                            user: req.user
                        });
                    });
                } else {
                    fs.mkdir('uploads/' + fields.project_id + '/', function (err){
                        if (err) next (err);
                    });
                    fs.rename(files.upload.path, 'uploads/' + fields.project_id + '/' + files.upload.name, function(err){
                        if(err) next (err);
                        res.render('profile.ejs',{
                            user:req.user
                        });
                    });
                }
            });
        });
    });

解决方案

You are trying to rename the file before the directory was created. Also, using fs.exists is not advisable, and the function will be deprecated in the future.

I made some changes in your code, you could use the path module to create the paths. Also, try creating the directory regardless if it already exists. If it exists, ignore the error code EEXIST.

The updated code:

// add this to the beggining
var path = require('path');

app.post('/uploads', function(req, res, next){
    var form = new formidable.IncomingForm();
    form.keepExtensions = true;
    form.parse(req, function(err, fields, files){
        if (err) next (err);
        fs.mkdir(path.resolve('uploads', fields.project_id), function (err) {
            if (err && err !== 'EEXIST') {
                next(err);
            } else {
                fs.rename(files.upload.path, path.resolve('uploads', fields.project_id, files.upload.name), function(err){
                    if(err) next (err);
                    res.render('profile.ejs',{
                        user:req.user
                    });
                });
            }
        });
    });
});

这篇关于文件上传:如果它不存在,创建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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