찾다

 >  Q&A  >  본문

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일 전499

모든 응답(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를 따로 브라우저로 방문하시면 다운로드가 가능한지 확인하실 수 있습니다. 백그라운드 서비스는 직접 404 가능하고 아직 프론트엔드와 백엔드의 공동 디버깅은 하지 맙시다.

    회신하다
    0
  • 취소회신하다