Storing images appropriately [closed]












0















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?










share|improve this 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
















0















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?










share|improve this 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














0












0








0








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












2 Answers
2






active

oldest

votes


















0














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

});
})





share|improve this answer
























  • 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



















1














Use multer middleware for file upload.



https://www.npmjs.com/package/multer






share|improve this answer






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














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

    });
    })





    share|improve this answer
























    • 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
















    0














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

    });
    })





    share|improve this answer
























    • 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














    0












    0








    0







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

    });
    })





    share|improve this answer













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

    });
    })






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 24 '18 at 11:41









    behzad.robotbehzad.robot

    18213




    18213













    • 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











    • 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













    1














    Use multer middleware for file upload.



    https://www.npmjs.com/package/multer






    share|improve this answer




























      1














      Use multer middleware for file upload.



      https://www.npmjs.com/package/multer






      share|improve this answer


























        1












        1








        1







        Use multer middleware for file upload.



        https://www.npmjs.com/package/multer






        share|improve this answer













        Use multer middleware for file upload.



        https://www.npmjs.com/package/multer







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 11:27









        Prayag C. PatelPrayag C. Patel

        687




        687















            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)