Home > Article > Web Front-end > How to solve the problem that the browser access path does not prompt for download but displays a new page
This time I will show you how to solve the problem of displaying a new page when the browser access path does not prompt for downloading. What are the precautions? Here are Let’s take a look at practical cases.
Taking the nodejs code as an example, I will jump to the browser's built-in playback page when accessing "/video".
When accessing "/frag_bunny.mp4", a download prompt will pop up.
Code:
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);
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to the php Chinese websiteOther related articles!
Related reading:
How to send emails through qq mailbox in python3
##How to use nodejs to implement single sign-on Demo
The above is the detailed content of How to solve the problem that the browser access path does not prompt for download but displays a new page. For more information, please follow other related articles on the PHP Chinese website!