nodejs--将浏览器默认打开的文件变成下载

var expressrequire("express")var appexpress();var fsrequire(fs);var pathrequire("path");app.ge

var express=require("express")
var app=express();
var fs = require('fs');
var path=require("path");
app.get("/upload",function(req,res){
    var filePath = path.join(__dirname, './');
    console.log(filePath)
    fs.readFile(filePath+"images/2010191.png", function(err, data){
        res.set({
            'Content-Type': 'application/octet-stream',  //告诉浏览器这是一个二进制文件
            'Content-Disposition': 'attachment; filename=upload.png'  //告诉浏览器这是一个附件要下载是png图片
        });
        res.end(data)
    })
    


})
app.listen(3000,function(){
    console.log("server is running")
})

发布者:admin,转转请注明出处:http://www.yc00.com/web/1742571310a4460462.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信