Home > Web Front-end > JS Tutorial > body text

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
1779 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(&#39;http&#39;);var fs = require(&#39;fs&#39;);var url = require(&#39;url&#39;);var routes = {//<====路由
    "/video"(request, response) {
        fs.readFile("frag_bunny.mp4", &#39;binary&#39;, function (err, data) {            if (err) {                console.log(err);
                response.writeHead(500, { &#39;Content-Type&#39;: &#39;text/html&#39; });
            } else {
                response.writeHead(200, { &#39;Content-Type&#39;: &#39;video/mp4&#39; });//<====mp4标识
                response.write(data, &#39;binary&#39;);
            }
            response.end();
        });
    },    "/frag_bunny.mp4"(request, response) {
        fs.readFile("frag_bunny.mp4", &#39;binary&#39;, function (err, data) {            if (err) {                console.log(err);
                response.writeHead(500, { &#39;Content-Type&#39;: &#39;text/html&#39; });
            } else {
                response.writeHead(200, { &#39;Content-Type&#39;: &#39;application/octet-stream&#39; });//<====文件流标识
                response.write(data, &#39;binary&#39;);
            }
            response.end();
        });
    },    "/"(request, response) {
        response.writeHead(200, { &#39;Content-Type&#39;: &#39;text/html&#39; }); 
        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, { &#39;Content-Type&#39;: &#39;text/html&#39; });
        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 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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template