简体   繁体   English

res.download 仅在一条快速路线中未设置正确的标头

[英]res.download not setting correct headers only in one express route

I have a main express route that once navigated to, should prompt a download of the specified file in res.download:我有一个主要的快速路线,一旦导航到,应该会提示下载 res.download 中的指定文件:

app.get('/download/:fileid', (req, res) => {
    var connection = mysql.createConnection({
        host     : 'localhost',
        user     : 'root',
        password : '',
        database : 'filesystem'
      });

    connection.connect(() => {
        console.log("Connection to database established");

    });
    connection.query(`SELECT * from files where fileid = '${req.params.fileid}'`, (error, results, fields) => {
        if (error) throw error;
        else if(results.length === 0){
            console.log("empty");
        }
        else{
            res.download(`./uploads/PFSk5x7bg.png`, 'PFSk5x7bg.png', (err) => {
                console.log(err);
            })
        }
    });

    connection.end();
})

However, when accessed, it simply displays the image on the webpage, and when I check the response headers, the content disposition header is missing.但是,访问时,它只是在网页上显示图像,当我检查响应标头时,缺少内容配置 header。 I have tried manually setting it with res.setheader, but still does not work.我尝试使用 res.setheader 手动设置它,但仍然无法正常工作。

I tried making another route just for testing:我尝试制作另一条路线仅用于测试:

app.get('/test', (req, res) => {
    res.download(`./uploads/PFSk5x7bg.png`, 'PFSk5x7bg.png', (err) => {
        console.log(err);
    })
})

and it works.... the content disposition header is there, the browser actually downloads the file.它有效.... 内容配置 header 在那里,浏览器实际上下载了文件。

Is anyone able to understand what I am doing wrong with my /download route?有谁能够理解我的 /download 路线做错了什么?

***UPDATE: I changed the name of the /download/:fileid route to /test/:fileid and it worked. ***更新:我将 /download/:fileid 路由的名称更改为 /test/:fileid 并且它有效。 I then changed it BACK to /download/:fileid and it now works... does anyone have any idea what could of caused this?然后我把它改回 /download/:fileid 并且它现在可以工作了......有没有人知道是什么原因造成的?

You have a few errors there, which can screw your debugging.你有一些错误,这可能会破坏你的调试。

  1. You have misplaced your connection.end() statement which will be called immediately after connection.query() .您放错了connection.end()语句,该语句将在connection.query()之后立即调用。 You should move it inside of connection.query 's callback.您应该将其移动到connection.query的回调中。

  2. Using relative paths instead of abolute ones.使用相对路径而不是绝对路径。 Try changing './uploads/PFSk5x7bg.png' to __dirname + '/uploads/PFSk5x7bg.png'尝试将'./uploads/PFSk5x7bg.png'更改为__dirname + '/uploads/PFSk5x7bg.png'

  3. Not using res.end() in the request where necessary may confuse browser's representation of response of your software.必要时不在请求中使用res.end()可能会混淆浏览器对软件响应的表示。

     if (error) { res.end(); throw error; }else if(results.length === 0){ res.end(); console.log("empty"); } else{ res.download(`./uploads/PFSk5x7bg.png`, 'PFSk5x7bg.png', (err) => { console.log(err); }) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM