时间:2021-07-01 10:21:17 帮助过:15人阅读
以nodejs代码为例,我将实现访问"/video"时跳转到浏览器的自带播放页面,
当访问"/frag_bunny.mp4"时弹出下载提示
代码:
var http = require('http');var fs = require('fs');var url = require('url');var routes = {//<====路由
"/video"(request, response) {
fs.readFile("frag_bunny.mp4", 'binary', function (err, data) { if (err) { console.log(err);
response.writeHead(500, { 'Content-Type': 'text/html' });
} else {
response.writeHead(200, { 'Content-Type': 'video/mp4' });//<====mp4标识
response.write(data, 'binary');
}
response.end();
});
}, "/frag_bunny.mp4"(request, response) {
fs.readFile("frag_bunny.mp4", 'binary', function (err, data) { if (err) { console.log(err);
response.writeHead(500, { 'Content-Type': 'text/html' });
} else {
response.writeHead(200, { 'Content-Type': 'application/octet-stream' });//<====文件流标识
response.write(data, 'binary');
}
response.end();
});
}, "/"(request, response) {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.write(`
<a target= "_blank" href="/video">打开页面显示播放界面</a>
<br />
<a target= "_blank" href="/frag_bunny.mp4">打开页面提示下载</a>
`);
response.end();
}, "/404"(request, response) {
response.writeHead(404, { 'Content-Type': 'text/html' });
response.write("404");
response.end();
}
}// 创建服务器http.createServer(function (request, response) { // 解析请求,包括文件名
var pathname = url.parse(request.url).pathname; // 输出请求的文件名
console.log("Request for " + pathname + " received.");
route = routes[pathname]
if (route) {
route(request, response);
} else {
routes["/404"](request, response);
}
}).listen(8889);相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!
相关阅读:
python3如何通过qq邮箱发送邮件
nodejs怎样通过jsonp来实现单点登录Demo
以上就是浏览器访问路径不提示下载而显示新页面应该如何解决的详细内容,更多请关注Gxl网其它相关文章!