How to solve the problem that the browser access path does not prompt for download but displays a new page

php中世界最好的语言
Release: 2018-03-12 11:35:50
Original
1680 people have browsed it

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(` 打开页面显示播放界面 
打开页面提示下载 `); 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);
Copy after login

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 websiteOtherrelated 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!