検索

ホームページ  >  に質問  >  本文

html5 - node.js下载失败,求大神解释

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

<!DOCTYPE html>
<html>
<head>
    <title>express 主页</title>
    <meta charset="utf-8">
    <script type="text/javascript" src="jquery.min.js"></script>
</head>
<body>
<p>
I love you!
</p>
<button>click there to download a pictutre</button>
<script type="text/javascript">
$(function(){
    $('button').click(function(){
            $.ajax({
                url:'/download',
                type:'get',
                success:function(result){
                    alert("Resquest has been received!");
                }
            })
    })
})

</script>

</body>
</html>

以下是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");


求解疑

伊谢尔伦伊谢尔伦2873日前502

全員に返信(2)返信します

  • 高洛峰

    高洛峰2017-04-17 15:19:43

    ダウンロード ボタンを

    として直接記述することをお勧めします リーリー

    http://www.expressjs.com.cn/4...
    res.download メソッドの最初のパラメータに注意してください

    リーリー

    サイズが大きいにもかかわらず、画像がぼやけすぎます。 。 。

    返事
    0
  • 天蓬老师

    天蓬老师2017-04-17 15:19:43

    index.html

    リーリー

    node.js

    リーリー

    ブラウザで個別に http://localhost:3000/download にアクセスして、バックグラウンド サービスを直接ダウンロードできますが、まだ準備ができていません。フロント エンドとバック エンドの共同デバッグは行わないでください。

    返事
    0
  • キャンセル返事