javascript - 使用node js上传文件并将表单信息存储在数据库(mysql)中

标签 javascript mysql node.js forms

我正在建立一个网站,用户需要填写一个表单,其中包含名字、姓氏、个人资料图片、号码等信息...我已经对表单进行了编码,并且正在寻找使用 Node js 的方法将图像上传到特定目录(或默认目录)并将用户填写的信息存储在我的数据库中。我使用 Node js 和 Express 以及 mysql。

更新:

(我使用了强大的包和util包)。 请注意,对于图像部分,我只需要路径(您将在输出部分看到它)。

表单代码:

<form method="post" action="upload" enctype="multipart/form-data">
    <fieldset>
       <input type="file" accept="image/*" name="image" required /><br/>
       <label for="Prenom">Prénom: </label>
       <input type="text" name="user_prenom" id="Prenom" required /><br/>
       <label for="Nom">Nom: </label>
       <input type="text" name="user_nom" id="Nom" required /><br/>
       <label for="Mail">E-Mail: </label>
       <input type="email" name="user_mail" id="Mail" required/><br/>
       <label for="Pays">Pays: </label>
       <input type="text" name="user_pays" id="Pays" required/><br/>
       <label for="Ville">Ville: </label>
       <input type="text" name="user_ville" id="Ville" required/><br/>
       <label for="Num">Numéro: </label>
       <input type="tel" name="user_telephone" id="Num" /><br/>
       <input type="submit"  />
  </fieldset>
 </form>

句柄:

router.post('/upload', function(req, res) {
processFormFieldsIndividual(req, res);
});

function processFormFieldsIndividual(req, res) {
//Store the data from the fields in your data store.
//The data store could be a file or database or any other store based
//on your application.
var fields = [];
var form = new formidable.IncomingForm();
form.uploadDir = "/public/photo_utilisateurs";
//Call back when each field in the form is parsed.
form.on('field', function (field, value) {
    fields[field] = value;
});
//Call back when each file in the form is parsed.
form.on('file', function (name, file) {
    fields[name] = file;
    //Storing the files meta in fields array.
    //Depending on the application you can process it accordingly.
});

//Call back for file upload progress.
form.on('progress', function (bytesReceived, bytesExpected) {
    var progress = {
        type: 'progress',
        bytesReceived: bytesReceived,
        bytesExpected: bytesExpected
    };
    //Logging the progress on console.
    //Depending on your application you can either send the progress to client
    //for some visual feedback or perform some other operation.
});

//Call back at the end of the form.
form.on('end', function () {

    res.writeHead(200, {
        'content-type': 'text/plain'
    });
    res.write('received the data:\n\n');
    res.end(util.inspect({
        fields: fields
    }));
});
// var user = JSON.parse(fields);
// connection.query('INSERT INTO Utilisateurs (user_nom, user_prenom, user_mail, user_phone, user_pays, user_ville) VALUES ("' + user.user_nom + '", "' + user.user_prenom + '", "' + user.user_mail + '", "' + user.user_pays + '", "' + user.user_ville + '", "' + user.user_telephone + '")',
//     function selectCb(err, results, fields) {
//         if (err) throw err;
//         else res.send('success');
// });
form.parse(req);
}

输出

收到数据:

{ fields: 
    [ user_prenom: 'dfw',
      user_nom: 'efwe',
      user_mail: 'efew@fref',
      user_pays: 'efwe',
      user_ville: 'efwe',
      user_telephone: '4380564324',
      image: File {
      domain: null,
      _events: {},
      _eventsCount: 0,
      _maxListeners: undefined,
      size: 518,
      path: '/tmp/upload_e611ea0745206682b26c208d816dc604',
      name: '1462507431_play_now_sign_youtube_video.svg',
      type: 'image/svg+xml',
      hash: null,
      lastModifiedDate: Mon May 09 2016 00:16:24 GMT-0400 (EDT),
     _writeStream: [Object] } 
    ] 
  } 

最佳答案

您可以简单地声明全局变量并将表单字段分配给它。并在您想要的地方使用它。

var data = util.inspec({
    data: fields
      });
    console.log(data);
   });

关于javascript - 使用node js上传文件并将表单信息存储在数据库(mysql)中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37108284/

相关文章:

javascript - 如何处理日期中的 onChange 值? react

javascript - 如何在nodejs中编写阻塞函数

php - 使用 php ajax jquery 在表中显示输入框的结果

php - Mysql连接类

node.js - 断言错误: expected stub to have been called with arguments 201

javascript - 快速应用程序中未捕获的语法错误

javascript - 如何在父 <optgroup> 中动态创建 <option> 元素

javascript - react forwardRef : ref and other props

php - 没有从 mysql_query() 得到合适的 php 字符串结果

javascript - Node.js 模块。导出未定义的空对象