上传图片并使用节点插入图片 url 到 mysql

Uploading images and inserting the image url to mysql with node

我有一个 node.js 应用程序可以上传图像并将它们存储在名为 uploads 的文件夹中。

我想将图像 url 存储在 mysql 数据库中,然后显示使用 html.

上传的所有图像

我正在使用 formidable 上传图片。

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);
            });
        });
    });
});

最简单的方法是在此函数中只保存图像的 URL:

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

根据您连接到数据库的方式,语法应类似于:

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);
        });
    });
});

编辑#1:

在 MySQL 中看起来类似于:

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);
        });
    });
});