node文件下载
首页 > >    作者:lininn   2018年6月28日 10:59 星期四   热度:897°   百度已收录  
时间:2018-6-28 10:59   热度:897° 
var express = require('express');
var fs = require('fs')
var path= require('path');
var cors = require('cors');


var app = express();
app.use(cors());  

var options = {
  dotfiles: 'ignore',
  etag: false,
  extensions: ['htm', 'html'],
  index: false,
  maxAge: '1d',
  redirect: false,
  setHeaders: function (res, path, stat) {
    res.set('x-timestamp', Date.now())
  }
}
app.use(express.static('public', options));


app.get('/download',function(req, res, next){
    var currDir = path.normalize(req.query.dir),
        fileName = req.query.name,
        currFile = path.join(currDir,fileName),
        fReadStream;
        console.log(currDir );
        console.log(fileName );
    fs.exists(currFile,function(exist) {
        if(exist){
            res.set({
                "Content-type":"application/octet-stream",
                "Content-Disposition":"attachment;filename="+encodeURI(fileName)
            });
            fReadStream = fs.createReadStream(currFile);
            fReadStream.on("data",function(chunk){res.write(chunk,"binary")});
            fReadStream.on("end",function () {
                res.end();
            });
        }else{
            res.set("Content-type","text/html");
            res.send("file not exist!");
            res.end();
        }
    });
});
app.listen(8088, function(){
    
    console.log('localhsot:8080')
});
二维码加载中...
本文作者:lininn      文章标题: node文件下载
本文地址:?post=173
版权声明:若无注明,本文皆为“覆手为雨”原创,转载请保留文章出处。
分享本文至:

返回顶部    首页    手机版本    后花园   会员注册   
版权所有:覆手为雨    站长: lininn