当我点击按钮时,我正在尝试使用Node.js(Express)和PUG(我是Web开发新手)下载文件 . 目前我有一个搜索引擎,我可以使用res.zip下载满足查询的所有文件(它们存储在./Datablob/name_of_docx.docx中) . 我还渲染了一个表格,其中包含有关搜索查询的所有文件的信息 . 我想在此表中添加一个可以下载每个单独文件的“下载”按钮 . 我的表中有一个列文件,它为我提供了文件路径 .

但是,我想定义一个函数,它接受在pug中定义的参数 . 我怎样才能得到我的结果?

这是我的代码:search.js

//To download all the files satisfying search request
var zip = require('express-zip');
var request = require('request');
var express = require('express');
var router = express.Router();

//THERE IS A router.get('/',function ....) with the 
//same schema than the router.post to res.render('search',searchResult:values)

router.post('/', function (req, res) {
    console.log(searchDocs(req.query.term));
    //it gets back a JSON file, with the metadata in value of the body 
    if (req.query.term != undefined)
{

    request.post(searchDocs(req.query.term),  function(error, response, body){

    console.info("search: " + response.statusCode);
    var values=JSON.parse(body).value;

    var doc = [];
    var datablob ='./Datablob/';

        for (var i=0;i<values.length;i++){
            console.log(datablob + values[i].file);
            var fileobject = {
                path:datablob + values[i].file,
                name:values[i].file
            };
            doc.push(fileobject);

        }
        res.zip(doc);
    })}
else {
    res.render('search');
}
});

我的search.pug代码:

div(class='row justify-content-center', id='DownloadBar')
//Button to download all docs
    form(class='form-inline justify-content-center', id='DownloadBarForm', method='post', action='')
            input(class='form-control mr-sm-2', id='downloadField', type='hidden', name='term', placeHolder='Search', aria-label='Search')
            button(class='btn btn-primary my-2 my-sm-0', id='downloadButton', type='submit') Download
             - var Resultlist = searchResult
             div
                 table.table.table-hover
                 thead
                     tr: CV
                         th Download
                         th File
                         th Text
                         th Name
                         th Email
                 tbody
                     each field in searchResult
                         tr
                             td="Click here to download " + field.file
                                 form(class='form-inline justify-content-center', method='post', action='')
                                     input(class="" name="filepath", value=field.file, type="hidden")
                                     button(class='btn btn-primary my-2 my-sm-0', id='downloadButton', type='submit', onclick=(downloadFile('./Datablob/' + field.file))) Download
                             td=field.file
                             td=field.chunk_text
                             td=field.candidate_name
                             td=field.email

我试图在脚本中的pug中定义downloadFile函数,但我不确定它是否是一个好方法,因为当我运行downloadFile不是函数的服务器时它返回:

script.
    $(function downloadFile(x){
        var filepath='./Datablob/';
        var fileselected = filepath + x;
        console.log(fileselected);
        $.ajax({
        url:fileselected,
        method:"post",
        success:function(){
        console.log('successful downloading');
        }
        });})

然后在我的search.js中我使用路径'/:value(*)'执行类似的router.post请求,但是我真的不确定它是否接受我在Pug输入中定义的值的参数 .

基本上,如何获取search.pug中循环中定义的field.file,然后将其发送到search.js中定义的post请求(res.download(file.field),其中file.field来自搜索 . pug)所以当我点击“下载”按钮时,只下载我感兴趣的文件?