node.js下载失败,求大神解释
高洛峰
高洛峰 2016-11-03 11:14:32
[HTML5讨论组]

问题对人有帮助,内容完整,我也想知道答案0问题没有实际价值,缺少关键内容,没有改进余地

使用express框架下载文件,但是失败了,直接看代码




    express 主页
    
    


I love you!
$(function(){     $('button').click(function(){             $.ajax({                 url:'/download',                 type:'get',                 success:function(result){                     alert("Resquest has been received!");                 }             })     }) }) 以下是node.js代码     var express = require("express"); var app = express(); app.use(express.static('public')); app.locals.title = "My app"; app.get("/",function(req,res){ console.log("resquest has been received!"); res.sendFile("public/index.html"); res.end(); }) app.get("/download",function(req,res){          res.download('public/example.jpg','example.jpg',function(err){         if(err)             console.log(err);         else             console.log("download successfully");     }); }) app.listen(3000); console.log("OK");

1.png

求解疑

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(1)
三叔

你的下载按钮我建议直接写成

下载


注意res.download方法的第一个参数

res.download('/report-12345.pdf', 'report.pdf', function(err){
  if (err) {
    // Handle error, but keep in mind the response may be partially-sent
    // so check res.headersSent
  } else {
    // decrement a download credit, etc.
  }
});

然后你的图片虽然尺寸很大,但是太模糊了。。。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

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