以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);
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
相关阅读:
以上就是浏览器访问路径不提示下载而显示新页面应该如何解决的详细内容,更多请关注php中文网其它相关文章!
![php中文网最新课程二维码](/static/images/article_wechat2021.jpg?1111)
声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
- 上一篇:Python爬虫浏览器标识库
- 下一篇:用JS做的点击复制效果
网友评论
文明上网理性发言,请遵守 新闻评论服务协议
我要评论