上载图片并将图片网址插入带有节点的mysql

时间:2019-03-13 22:19:03

标签: mysql node.js express formidable

我有一个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);
            });
        });
    });
});

1 个答案:

答案 0 :(得分:0)

最简单的方法是在此函数中保存图像的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);
        });
    });
});