Search code examples
mysqlnode.jsexpressformidable

Uploading images and inserting the image url to mysql with node


I have a node.js application which uploads images and stores them in a folder called uploads.

I want to store the image url inside of mysql database and then display all of the images which where uploaded with html.

Im using formidable to upload the images.

app.get('/upload', function (req, res){
    res.sendFile(__dirname + '/index.html');
});

app.post('/up', function (req, res){
    var form = new formidable.IncomingForm();

    form.parse(req);

    form.on('fileBegin', function (name, file){
        file.path = __dirname + '/uploads/' + file.name;
    });



 form.on('file', function (name, file) {
        console.log('Uploaded ' + file.name);

        connection.query('SELECT * FROM images', (err, results) => {
            // Throw error if find function fails
            if (err) return (new Error(err));

            // Throw error if there is already a file with that name
            if (results[0]) return (new Error('File with that name already exists, please choose another name'));

            connection.query('INSERT INTO images', (error, results) => {


                // Throw error if save function fails
                if (err) return (new Error(err));

                // Throw error if you cannot verifty the save occured.

                if (results.affectedRows !== 1) return (new Error('There was an unexpected error. Please try again'));
                // Send Log to signal successfull upload
                console.log('Uploaded ' + file.name);
            });
        });
    });
});

Solution

  • The easiest way would be to just save the URL of the image in this function:

    form.on('file', function (name, file){
         console.log('Uploaded ' + file.name);
    });
    

    Depending on how you are connecting to your database, the syntax should look similar to this:

    form.on('file', function (name, file){
        database.find({file: file.path}, (err, result) => {
            // Throw error if find function fails
            if(err) return(new Error(err));
    
            // Throw error if there is already a file with that name
            if(result) return(new Error('File with that name already exists, please choose another name'));
    
            database.save({file: file.path}, (err, result) => {
                // Throw error if save function fails
                if(err) return(new Error(err));
    
                // Throw error if you cannot verifty the save occured.
                if(result.count !== 1) return(new Error('There was an unexpected error. Please try again'));
    
                // Send Log to signal successfull upload
                console.log('Uploaded ' + file.name);
            });
        });
    });
    

    EDIT #1:

    In MySQL that would look similar to this:

    form.on('file', function (name, file){
        connection.query('SQL SELECT QUERY HERE', (err, results) => {
            // Throw error if find function fails
            if(err) return(new Error(err));
    
            // Throw error if there is already a file with that name
            if(results[0]) return(new Error('File with that name already exists, please choose another name'));
    
            connection.query('SQL INSERT QUERY HERE', (error, results) => {
                // Throw error if save function fails
                if(err) return(new Error(err));
    
                // Throw error if you cannot verifty the save occured.
                if(results.affectedRows !== 1) return(new Error('There was an unexpected error. Please try again'));
    
                // Send Log to signal successfull upload
                console.log('Uploaded ' + file.name);
            });
        });
    });