Reputation: 107
I have a simple HTML form that lets the user upload a file. That than gets posted with auth/post/ on submission.
My route looks like this:
const express = require('express');
const authController = require('../controllers/auth');
const router = express.Router();
const multer = require('multer');
function checkFileType(file, cb){
const filetypes = /jpeg|jpg|png|gif|mp3|ogg|aac|wav/;
if (req.fileValidationError) {
return res.send(fileValidationError);
}
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
const mimetype = filetypes.test(file.mimetype);
if(mimetype && extname){
return cb(null,true);
} else {
cb('Format not allowed!');
}
}
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null,'./user_uploads/')
},
fileFilter: function(req, file, cb){
checkFileType(file, cb);
},
filename: function (req, file, cb) {
let ext = file.originalname.substring(file.originalname.lastIndexOf('.'), file.originalname.length);
cb(null, Date.now() + ext)
}
});
const upload = multer({
storage: storage
}).single('user_file');
router.post('/post', upload, function(req, res, next){ res.locals.storage = storage; next();}, authController.post);
module.exports = router;
It checks that you actually put something in, that it's in the right formats, changes the filename and saves it to a temporary folder when you use 'upload'.
I now want to get the storage variable aka the filepath/name/extension to my authController.post.
It's a controller with exports where everything else gets saved to the database. How would I pass the path variable to it?
For reference, it looks like this (I need to put the path into user_path: _______):
exports.post = (req, res, next) => {
let { name, title, cat, con, user_file } = req.body;
const time = new Date().toJSON().slice(0, 19).replace('T', ' ');
let cat_id = 0;
if(req.files) user_file.fileName = req.file.filename;
db.query("SELECT id FROM cat WHERE cat_name = ?", [cat], async function(err, results) {
if (!err)
cat_id = results[0].id;
else
console.log(err);
const ID = await promisify(jwt.verify)(req.cookies.scrooc, process.env.jwtsecret);
const id = ID.id;
db.query('INSERT INTO posts SET ?', {user_id: id, cat_id: cat_id, time: time, title: title, content: con, user_file: res.locals.storage, reply: 0 }, (error, results) => {
if(error)
console.log(error);
else
res.status(200).redirect('/post');
});
});
};
Upvotes: 0
Views: 1242
Reputation: 107
I ended up just doing the full function in the route. Might not be that clean but it worked.
Upvotes: 0
Reputation: 251
I think you should try writing a textfield (QLineEdit in Python), setting the filename path to the textfield and then saving the content of the textfield to your database using an insert statement during insertion of values into your databse. For example, using Python, here is one way you can achieve that: the getfile is the name of my function, you can name yours anything but remember to call it. The photoinfo is the name of my QLineEdit, in Java, it is called a JTextField if I still remember. So try to use the logic in my code to see if it can resolve yours.
''' def getfile(self):
self.file_name = QtWidgets.QFileDialog.getOpenFileName(None, "Open", "", "All Files (*);;JPG Files(*.jpg)")
try:
if self.file_name[0] != '':
print("success")
self.photoInfo.setText(self.file_name[0])
else:
print("error")
except Exception as e:
print(e)
Upvotes: 1