【发布时间】: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