美文网首页
Node.js 实现下载

Node.js 实现下载

作者: 飞鱼_JS | 来源:发表于2017-07-14 10:04 被阅读0次
//html
<a href="/downData" download>下载</a>
//服务器
var http = require("http");
var fs = require("fs");
http.createServer((req,res)=>{
    if(req.url == "/favicon.ico"){
        return;
    }
    if(req.url == "/"){
        fs.readFile("./index.html",(err,data)=>{
            res.writeHead(200,{"Content-type":"text/html;charset=utf-8"})
            res.end(data)
        })
    }
    if(req.url == "/downData"){
        fs.readFile("./index.html",(err,data)=>{
            res.writeHead(200,{
                "Content-type":"application/octet-stream",
                "Content-Disposition":"attachment; filename=index.excell"
            })
            res.end(data)
        })
    }
}).listen(8000)

相关文章

网友评论

      本文标题:Node.js 实现下载

      本文链接:https://www.haomeiwen.com/subject/tyjnhxtx.html