1

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

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>

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:

enter image description 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! :)

Blake Lewis
  • 113
  • 3
  • 11
  • where's your js file located? can you perhaps try `../public/files/CLPs/minor_CLP.pdf` instead (assuming it's in app folder)? – kkesley Apr 11 '18 at 23:53
  • @KendrickKesley Yes, my js file is located in the app folder. I get the same error when I try `../public/files/CLPs/` – Blake Lewis Apr 11 '18 at 23:59
  • What if you upload the files without any directory? Does it work? e.g. just minor_CLP.pdf – kkesley Apr 12 '18 at 00:14
  • @KendrickKesley when I try `clpFile.mv('/' + clpName, function(err) ` I get this error message: `Error: EACCES: permission denied, open '/minor_CLP.pdf'` – Blake Lewis Apr 12 '18 at 04:19
  • hmmm what if you omit the / ? so it will be in the same directory as your code – kkesley Apr 12 '18 at 08:27
  • @KendrickKesley When I try `clpFile.mv('' + clpName, function(err)`, it moves the file to the main directory of my project. So it is just in my main project folder. I also want to rename the file to something I can query later, which I thought I was doing that with clpName, but the file is just saved as whatever name it is uploaded as. – Blake Lewis Apr 12 '18 at 20:22
  • Hm... can you show me the location of your js code? preferably using the hierarchy picture as you've shown the CLPs folder in your question – kkesley Apr 13 '18 at 01:32
  • @KendrickKesley I edited the post to show an updated project directory. You can see where `tickets.pdf` was uploaded as well as the location of my routes.js file with all of my GET/POST functions. I also edited to add my code from my server.js file. I set an `express.static` directory for my images, so maybe this is affecting it? – Blake Lewis Apr 13 '18 at 05:26
  • 1
    ah! your app is on the root folder! try using `clpFile.mv('public/files/CLPs/' + clpName, function(err)` – kkesley Apr 13 '18 at 05:39
  • @KendrickKesley that worked perfectly! Thank you! – Blake Lewis Apr 13 '18 at 05:55
  • 1
    not a problem! I provided the answer in case anyone has the same problem – kkesley Apr 13 '18 at 05:58

1 Answers1

2

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.

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

kkesley
  • 3,258
  • 1
  • 28
  • 55