【问题标题】:How to get file path in node js and pass to the DB request如何在节点js中获取文件路径并传递给数据库请求
【发布时间】:2020-07-25 09:06:40
【问题描述】:

我使用 Node.js express + PostgreSQL

如何在 BD 查询中通过 Node.js 将路径传递给我的 CSV 文件?

我有一个 html 表单,它有文件输入,比如 -

<form method="post" action="import_csv" target="_blank" enctype="multipart/form-data" >
<p><input type="file" name="csv_file">
<input type="submit" value="submit"></p>
</form>

之后,我将这个post请求重定向到了控制器(服务器)的以下方法。

app.post("/import_csv_file",urlencodedParser,function(req,res) {

    var config = {
        user:'postgres',
        database:'mybd',
        password: '1',
        host:'localhost',
        port:5432,
        max:10,
        idleTimeoutMillis: 30000
    }
    var pool = new pg.Pool(config)
    pool.connect(function(err, client, done){
        console.log("teacher")
        if(err){
            return console.error('error')
        }
        file_path = req.body.csv_file
        console.log(file_path)


        client.query("select * from import_csv($1,$2)",[log2,req.body.csv_file], function(err, result) {
            done()
        req.session.user2 = result
        if (err){
            res.end()
            return console.error("error")
       }
        res.render('import_csv_file',{jour:result}})
      })
       })

我在这里得到的结果是 - undefined (console.log(req.body.csv_file))

如何将我的 CSV 文件的路径传递给数据库查询,以便一切正常

【问题讨论】:

  • 先尝试控制台记录 req.body
  • @Syntle 我不明白你的意思。 file_path = req.body.csv_file console.log(file_path)。日志给了我结果 - 未定义
  • enctype="multipart/form-data"urlencodedParser 不匹配
  • @Quentin 我开始看到文件名了。但是如何传递路径以便数据库查询了解从哪里获取文件? file_path = req.body.csv_file console.log(file_path) 结果 - db.csv 我删除了 - enctype="multipart/form-data"
  • 你不能。文件输入上传文件。它不能帮助服务器知道文件在访问者计算机上的位置。它无法读取访问者的硬盘。如果可以,世界上就没有隐私了。

标签: html node.js postgresql file express


【解决方案1】:

感谢大家的帮助!

安装程序 multer 和下一步

    const path = require('path')
    var multer = require('multer');
    const upload = multer({ dest: '/tmp/' });

    app.post("/import_csv_file",upload.any(),function(req,res) {

        var config = {
            user:'postgres',
            database:'mybd',
            password: '1',
            host:'localhost',
            port:5432,
            max:10,
            idleTimeoutMillis: 30000
        }
        var pool = new pg.Pool(config)
        pool.connect(function(err, client, done){
            console.log("teacher")
            if(err){
                return console.error('error')
            }
           console.log(req.body);
  console.log(req.files);

  const file = req.files[0];
 console.log(file)
 console.log(file.path)


            client.query("select * from import_csv($1,$2)",[log2,file.path], function(err, result) {
                done()
            req.session.user2 = result
            if (err){
                res.end()
                return console.error("error")
           }
            res.render('import_csv_file',{jour:result}})
          })
           })

和html

<form method="post" action="inside_teacher_import_ocenka" target="_blank" enctype="multipart/form-data">
<p><input type="file" name="csv_ocenka">
<input type="submit" value="Отправить"></p>
</form>

【讨论】:

    猜你喜欢
    • 2022-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 2021-03-07
    • 2017-10-16
    • 2019-01-29
    • 2021-07-13
    相关资源
    最近更新 更多