node.js - nodejs中res.download()的的status code 是200 ok,但响应的都是乱码,求解!
大家讲道理
大家讲道理 2017-04-17 15:53:09
[Node.js讨论组]

router.use("/download",function (req, res, next) {

res.download('d:/myFile/','模板.xlsx',function (err) {
    if(err){
        res.send(err);
    }else{
        res.send(true);
    }
});

});

后来又返回了以下这个
code:"ENOENT"
errno:-4058
expose:false
path:"E:企业版开通模板.xlsx"
status:404
statusCode:404
syscall:"stat"
res.download()的status code 是200 ok,但返回值都是乱码,这又该咋办呢?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(1)
迷茫

你的路径是不是有问题,具体的返回值,要看接口怎么定义{"code":"EISDIR"}?

express是在服务端运行的程序,具体要下载到那个目录,是客户端来决定的,比如:浏览器设定默认下载到桌面。

res.download的API如下
res.download(path, [filename], [fn])
Transfer the file at path as an “attachment”, typically browsers will prompt the user for download. The Content-Disposition “filename=” parameter, aka the one that will appear in the brower dialog is set to path by default, however you may provide an override filename.

When an error has ocurred or transfer is complete the optional callback fn is invoked. This method uses res.sendfile() to transfer the file.

res.download('/report-12345.pdf');

res.download('/report-12345.pdf', 'report.pdf');

res.download('/report-12345.pdf', 'report.pdf', function(err){
  if (err) {
    // handle error, keep in mind the response may be partially-sent
    // so check res.headerSent
  } else {
    // decrement a download credit etc
  }
});
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号