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:
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! :)