使用Express-fileupload上传文件时没有此类文件或目录错误-Node.js [英] No such file or directory error when uploading a file using Express-fileupload - Node.js

查看:53
本文介绍了使用Express-fileupload上传文件时没有此类文件或目录错误-Node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试上传pdf文件,以我可以查询的名称将该文件存储在我的目录中,然后将此名称存储在SQL表中.我正在使用Express-fileupload,但是正在获取文件,但是当我尝试使用 .mv 移动文件时,出现了 Error:ENOENT:没有此类文件或目录,请打开"/public/files/CLPs/minor_CLP.pdf'错误.这是我的post函数的代码:

I am currently trying to upload a pdf file, store this file in my directory under a name I can query, and store this name in an SQL table. I am using Express-fileupload, and I am getting the file, but when I try to move the file using .mv I get an Error: ENOENT: no such file or directory, open '/public/files/CLPs/minor_CLP.pdf' error. Here is the code for my post function:

app.post('/uploadCLP', isLoggedIn, function(req, res) {
    var communityID = req.user.communityID;

    console.log(req.files);

    var clpFile = req.files.clpFile;
    var clpName = communityID + "_CLP.pdf";

    clpFile.mv('/public/files/CLPs/' + clpName, function(err) {
        if(err)
            return console.log(err);
    });

    var updateQuery = ("UPDATE Community SET clpFile = ? WHERE communityID = ?");
    connection.query(updateQuery, [clpName, communityID], function(err, rows) {
        if(err) throw err;
        res.redirect(302, '/proposal');
    });
});

这是我的EJS文件中的表单,其中该函数被调用:

And here is the form from my EJS file where the function is called:

<div class="well">
    <form action="/uploadCLP" method="post" enctype="multipart/form-data">
        <div class="form-group">
            <label>Community Learning Plan</label>
            <p>Only upload Community Learning Plans in PDF format.</p>
            <input type="file" accept=".pdf" class="form-control" name="clpFile">
        </div>
</div>

编辑后还显示了我的server.js文件:

Edited to show my server.js file as well:

// server.js

// get all the tools we need
var express  = require('express');
var session  = require('express-session');
var favicon = require('express-favicon');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var morgan = require('morgan');

var upload = require('express-fileupload');
var app      = express();
var port     = process.env.PORT || 1848;

var passport = require('passport');
var flash    = require('connect-flash');


require('./config/passport')(passport); // pass passport for configuration

app.use('/public', express.static('public'));

// set up our express application
app.use(morgan('dev')); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(bodyParser.json());

app.set('view engine', 'ejs'); // set up ejs for templating
app.use(upload());

// required for passport
app.use(session({
    secret: 'vidyapathaisalwaysrunning',
    resave: true,
    saveUninitialized: true
 } )); // session secret

app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session


require('./app/routes.js')(app, passport); 

app.listen(port);
console.log('Server running on port ' + port);

此外,您还可以在这里看到我的项目目录:

Additionally, you can see my project directory here:

我不确定问题是否出在我要设置文件保存目录的方式上,还是问题在于我试图使用其他名称保存文件.任何帮助或提示将不胜感激!:)

I'm not sure if the problem is with how I'm setting the directory for the file to save in or if the problem is I'm trying to save the file under a different name. Any help or tips would be much appreciated! :)

推荐答案

由于路径开头使用/,因此它将搜索该文件夹的根路径,而不是项目文件夹本身.

Because the path is using / in the beginning, it searches for the root path of that folder which is not the project's folder itself.

因为脚本位于项目的根文件夹中,所以正确的路径应为'public/files/CLPs/'

because the script is in the root folder of the project, the correct path should be 'public/files/CLPs/'

这篇关于使用Express-fileupload上传文件时没有此类文件或目录错误-Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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