Как загрузить файл с помощью node.js formidable-express и сохранить путь URL MySQL

Как этот код, как посмотреть


**upload first and save path?**
var getConnection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: null,
    database: 'filetoupload'
  })
  .post(function(req, res, err) {
      var params = {
        name: req.body.filename,
        file: req.body.file
      }
      if (pathname == 'new/file') {
        var form = new formidable.IncomingForm()
        form.parse(req, function(req, err, fields, files) {
            var oldpath = 'path/upload/filename'
            var newpath = 'path/upload/newpath'

            fs.rename(oldpath, newpath) {
              if (err) throw err
              console.log("File upload")
              res.end()
            })
        }
        var query = "insert into `files` (name,path) SET ?"

        getConnection.query(query, params, function() {
          if (err) throw err
          console.log("1 record has been inserted %s")

        })
      )
    }

  })
<html>

<head>
  <title>Files to upload</title>
</head>

<body>
  <form method="post" enctype="multipart-form/data">
    <input type="text" name="name" placeholder="name to file upload">
    <input type="file" name="filename">
    <button type="submit">Upload File</button>
  </form>
</body>

</html>

0 ответов

Другие вопросы по тегам