Storing images appropriately [closed]
I want to store some images using mysql
and nodejs
. I have read that it is not a good idea to store them directly in the database but that I should store the path to the image in the database, and store the image in the filesystem.
Say a user uploads an image, how, exactly, do I store the image in the filesystem?
mysql node.js
closed as too broad by Madhur Bhaiya, Gert Arnold, Billal Begueradj, jww, Pearly Spencer Nov 24 '18 at 14:18
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I want to store some images using mysql
and nodejs
. I have read that it is not a good idea to store them directly in the database but that I should store the path to the image in the database, and store the image in the filesystem.
Say a user uploads an image, how, exactly, do I store the image in the filesystem?
mysql node.js
closed as too broad by Madhur Bhaiya, Gert Arnold, Billal Begueradj, jww, Pearly Spencer Nov 24 '18 at 14:18
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
You could use multer middleware and it will do it here's more info. github.com/expressjs/multer
– vitomadio
Nov 24 '18 at 11:19
Thanks, but the link is not working
– Carlo Jacobs
Nov 24 '18 at 11:20
It depends to some (small) extent on file size. If the files are below, say, 100k, then storing them in the db seems to work pretty well
– Strawberry
Nov 24 '18 at 11:52
add a comment |
I want to store some images using mysql
and nodejs
. I have read that it is not a good idea to store them directly in the database but that I should store the path to the image in the database, and store the image in the filesystem.
Say a user uploads an image, how, exactly, do I store the image in the filesystem?
mysql node.js
I want to store some images using mysql
and nodejs
. I have read that it is not a good idea to store them directly in the database but that I should store the path to the image in the database, and store the image in the filesystem.
Say a user uploads an image, how, exactly, do I store the image in the filesystem?
mysql node.js
mysql node.js
asked Nov 24 '18 at 11:05
Carlo JacobsCarlo Jacobs
83
83
closed as too broad by Madhur Bhaiya, Gert Arnold, Billal Begueradj, jww, Pearly Spencer Nov 24 '18 at 14:18
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by Madhur Bhaiya, Gert Arnold, Billal Begueradj, jww, Pearly Spencer Nov 24 '18 at 14:18
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
You could use multer middleware and it will do it here's more info. github.com/expressjs/multer
– vitomadio
Nov 24 '18 at 11:19
Thanks, but the link is not working
– Carlo Jacobs
Nov 24 '18 at 11:20
It depends to some (small) extent on file size. If the files are below, say, 100k, then storing them in the db seems to work pretty well
– Strawberry
Nov 24 '18 at 11:52
add a comment |
You could use multer middleware and it will do it here's more info. github.com/expressjs/multer
– vitomadio
Nov 24 '18 at 11:19
Thanks, but the link is not working
– Carlo Jacobs
Nov 24 '18 at 11:20
It depends to some (small) extent on file size. If the files are below, say, 100k, then storing them in the db seems to work pretty well
– Strawberry
Nov 24 '18 at 11:52
You could use multer middleware and it will do it here's more info. github.com/expressjs/multer
– vitomadio
Nov 24 '18 at 11:19
You could use multer middleware and it will do it here's more info. github.com/expressjs/multer
– vitomadio
Nov 24 '18 at 11:19
Thanks, but the link is not working
– Carlo Jacobs
Nov 24 '18 at 11:20
Thanks, but the link is not working
– Carlo Jacobs
Nov 24 '18 at 11:20
It depends to some (small) extent on file size. If the files are below, say, 100k, then storing them in the db seems to work pretty well
– Strawberry
Nov 24 '18 at 11:52
It depends to some (small) extent on file size. If the files are below, say, 100k, then storing them in the db seems to work pretty well
– Strawberry
Nov 24 '18 at 11:52
add a comment |
2 Answers
2
active
oldest
votes
:D Other than multer you can also try "express-fileupload" if you're using express.
Adds req.files to your requests ( :D easier to work with imo)
https://www.npmjs.com/package/express-fileupload
https://github.com/richardgirges/express-fileupload/tree/master/example#basic-file-upload
const fileUpload = require('express-fileupload');
expressApp.use(fileUpload());
then in your routes:
app.post('/upload', function(req, res) {
console.log(req.files.foo); // the uploaded file object
});
I also use "sharp" for resizing images:
https://www.npmjs.com/package/sharp
Example(i've not tested this code just copied and change a bit from a project):
expressApp.post('/submit-form',(req,res)=>{
let f = req.files.profileImage;
//its usually a good idea to rename files uploaded to sth unique using DateTime with their original name (or instead of their original file name)
const fileName = (f.name.substring(0, f.name.lastIndexOf('.')) + '-' + Date.now().toString()).replace(' ','');
const fileFormat = f.name.substring(f.name.lastIndexOf('.'), f.name.length);
const filePath = path.resolve('public/media') + '/' + folder + fileName + fileFormat;
f.mv((err)=>{
if(err)
{
console.log('sth went wrong while uploading image!');
return;
}
//HERE STORE filePath to Database or sth:
});
})
Alright, but how do I then continue to store the image?
– Carlo Jacobs
Nov 24 '18 at 12:02
Using mv function file is stored in your database after the callback. You can put your own logic and code after this comment: //HERE STORE filePath to Database or sth:
– behzad.robot
Nov 24 '18 at 12:31
Ok I get it now, thx!
– Carlo Jacobs
Nov 24 '18 at 13:02
add a comment |
Use multer middleware for file upload.
https://www.npmjs.com/package/multer
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
:D Other than multer you can also try "express-fileupload" if you're using express.
Adds req.files to your requests ( :D easier to work with imo)
https://www.npmjs.com/package/express-fileupload
https://github.com/richardgirges/express-fileupload/tree/master/example#basic-file-upload
const fileUpload = require('express-fileupload');
expressApp.use(fileUpload());
then in your routes:
app.post('/upload', function(req, res) {
console.log(req.files.foo); // the uploaded file object
});
I also use "sharp" for resizing images:
https://www.npmjs.com/package/sharp
Example(i've not tested this code just copied and change a bit from a project):
expressApp.post('/submit-form',(req,res)=>{
let f = req.files.profileImage;
//its usually a good idea to rename files uploaded to sth unique using DateTime with their original name (or instead of their original file name)
const fileName = (f.name.substring(0, f.name.lastIndexOf('.')) + '-' + Date.now().toString()).replace(' ','');
const fileFormat = f.name.substring(f.name.lastIndexOf('.'), f.name.length);
const filePath = path.resolve('public/media') + '/' + folder + fileName + fileFormat;
f.mv((err)=>{
if(err)
{
console.log('sth went wrong while uploading image!');
return;
}
//HERE STORE filePath to Database or sth:
});
})
Alright, but how do I then continue to store the image?
– Carlo Jacobs
Nov 24 '18 at 12:02
Using mv function file is stored in your database after the callback. You can put your own logic and code after this comment: //HERE STORE filePath to Database or sth:
– behzad.robot
Nov 24 '18 at 12:31
Ok I get it now, thx!
– Carlo Jacobs
Nov 24 '18 at 13:02
add a comment |
:D Other than multer you can also try "express-fileupload" if you're using express.
Adds req.files to your requests ( :D easier to work with imo)
https://www.npmjs.com/package/express-fileupload
https://github.com/richardgirges/express-fileupload/tree/master/example#basic-file-upload
const fileUpload = require('express-fileupload');
expressApp.use(fileUpload());
then in your routes:
app.post('/upload', function(req, res) {
console.log(req.files.foo); // the uploaded file object
});
I also use "sharp" for resizing images:
https://www.npmjs.com/package/sharp
Example(i've not tested this code just copied and change a bit from a project):
expressApp.post('/submit-form',(req,res)=>{
let f = req.files.profileImage;
//its usually a good idea to rename files uploaded to sth unique using DateTime with their original name (or instead of their original file name)
const fileName = (f.name.substring(0, f.name.lastIndexOf('.')) + '-' + Date.now().toString()).replace(' ','');
const fileFormat = f.name.substring(f.name.lastIndexOf('.'), f.name.length);
const filePath = path.resolve('public/media') + '/' + folder + fileName + fileFormat;
f.mv((err)=>{
if(err)
{
console.log('sth went wrong while uploading image!');
return;
}
//HERE STORE filePath to Database or sth:
});
})
Alright, but how do I then continue to store the image?
– Carlo Jacobs
Nov 24 '18 at 12:02
Using mv function file is stored in your database after the callback. You can put your own logic and code after this comment: //HERE STORE filePath to Database or sth:
– behzad.robot
Nov 24 '18 at 12:31
Ok I get it now, thx!
– Carlo Jacobs
Nov 24 '18 at 13:02
add a comment |
:D Other than multer you can also try "express-fileupload" if you're using express.
Adds req.files to your requests ( :D easier to work with imo)
https://www.npmjs.com/package/express-fileupload
https://github.com/richardgirges/express-fileupload/tree/master/example#basic-file-upload
const fileUpload = require('express-fileupload');
expressApp.use(fileUpload());
then in your routes:
app.post('/upload', function(req, res) {
console.log(req.files.foo); // the uploaded file object
});
I also use "sharp" for resizing images:
https://www.npmjs.com/package/sharp
Example(i've not tested this code just copied and change a bit from a project):
expressApp.post('/submit-form',(req,res)=>{
let f = req.files.profileImage;
//its usually a good idea to rename files uploaded to sth unique using DateTime with their original name (or instead of their original file name)
const fileName = (f.name.substring(0, f.name.lastIndexOf('.')) + '-' + Date.now().toString()).replace(' ','');
const fileFormat = f.name.substring(f.name.lastIndexOf('.'), f.name.length);
const filePath = path.resolve('public/media') + '/' + folder + fileName + fileFormat;
f.mv((err)=>{
if(err)
{
console.log('sth went wrong while uploading image!');
return;
}
//HERE STORE filePath to Database or sth:
});
})
:D Other than multer you can also try "express-fileupload" if you're using express.
Adds req.files to your requests ( :D easier to work with imo)
https://www.npmjs.com/package/express-fileupload
https://github.com/richardgirges/express-fileupload/tree/master/example#basic-file-upload
const fileUpload = require('express-fileupload');
expressApp.use(fileUpload());
then in your routes:
app.post('/upload', function(req, res) {
console.log(req.files.foo); // the uploaded file object
});
I also use "sharp" for resizing images:
https://www.npmjs.com/package/sharp
Example(i've not tested this code just copied and change a bit from a project):
expressApp.post('/submit-form',(req,res)=>{
let f = req.files.profileImage;
//its usually a good idea to rename files uploaded to sth unique using DateTime with their original name (or instead of their original file name)
const fileName = (f.name.substring(0, f.name.lastIndexOf('.')) + '-' + Date.now().toString()).replace(' ','');
const fileFormat = f.name.substring(f.name.lastIndexOf('.'), f.name.length);
const filePath = path.resolve('public/media') + '/' + folder + fileName + fileFormat;
f.mv((err)=>{
if(err)
{
console.log('sth went wrong while uploading image!');
return;
}
//HERE STORE filePath to Database or sth:
});
})
answered Nov 24 '18 at 11:41
behzad.robotbehzad.robot
18713
18713
Alright, but how do I then continue to store the image?
– Carlo Jacobs
Nov 24 '18 at 12:02
Using mv function file is stored in your database after the callback. You can put your own logic and code after this comment: //HERE STORE filePath to Database or sth:
– behzad.robot
Nov 24 '18 at 12:31
Ok I get it now, thx!
– Carlo Jacobs
Nov 24 '18 at 13:02
add a comment |
Alright, but how do I then continue to store the image?
– Carlo Jacobs
Nov 24 '18 at 12:02
Using mv function file is stored in your database after the callback. You can put your own logic and code after this comment: //HERE STORE filePath to Database or sth:
– behzad.robot
Nov 24 '18 at 12:31
Ok I get it now, thx!
– Carlo Jacobs
Nov 24 '18 at 13:02
Alright, but how do I then continue to store the image?
– Carlo Jacobs
Nov 24 '18 at 12:02
Alright, but how do I then continue to store the image?
– Carlo Jacobs
Nov 24 '18 at 12:02
Using mv function file is stored in your database after the callback. You can put your own logic and code after this comment: //HERE STORE filePath to Database or sth:
– behzad.robot
Nov 24 '18 at 12:31
Using mv function file is stored in your database after the callback. You can put your own logic and code after this comment: //HERE STORE filePath to Database or sth:
– behzad.robot
Nov 24 '18 at 12:31
Ok I get it now, thx!
– Carlo Jacobs
Nov 24 '18 at 13:02
Ok I get it now, thx!
– Carlo Jacobs
Nov 24 '18 at 13:02
add a comment |
Use multer middleware for file upload.
https://www.npmjs.com/package/multer
add a comment |
Use multer middleware for file upload.
https://www.npmjs.com/package/multer
add a comment |
Use multer middleware for file upload.
https://www.npmjs.com/package/multer
Use multer middleware for file upload.
https://www.npmjs.com/package/multer
answered Nov 24 '18 at 11:27
Prayag C. PatelPrayag C. Patel
687
687
add a comment |
add a comment |
You could use multer middleware and it will do it here's more info. github.com/expressjs/multer
– vitomadio
Nov 24 '18 at 11:19
Thanks, but the link is not working
– Carlo Jacobs
Nov 24 '18 at 11:20
It depends to some (small) extent on file size. If the files are below, say, 100k, then storing them in the db seems to work pretty well
– Strawberry
Nov 24 '18 at 11:52