Mp4下载导致浏览器播放文件而不是下载 [英] Mp4 Download causes browser to play file instead of download

查看:1038
本文介绍了Mp4下载导致浏览器播放文件而不是下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站我流式传输用户的mp4内容。我也允许用户下载。
但是在Chrome中,似乎是自动在内部播放器播放文件,而不是下载文件。

In my website I stream users mp4 content. I also allow users to download. However in Chrome it seems to automatically play the file in an internal player instead of downloading the file.

如何强制浏览器下载文件

How do I force the browser to download the file instead.

感谢并感谢
Craig

Regards and thanks Craig

推荐答案

你必须使用HTTP标头 Content-Disposition Content-Type:application / force-download ,这将强制浏览器下载内容,而不是在那里显示。

You have to use the HTTP header "Content-Disposition" and 'Content-Type: application/force-download' which will force browser to download the content instead of displaying it there.

根据您实现的服务器端语言不同,如果

Depending upon the server side language you are having the implementation differs. In case of

PHP:

    header('Content-Disposition: attachment; filename="'.$nameOfFile.'"');

将为您做这项工作。

为了简化和推广所有文件,您可能需要编写一种将链接路由到可下载内容的方法。

Ofcourse to simplify and generalize this for all your files, you may need to write a method which will route a link to downloadable content.

您可以在html中显示的链接将如下所示:

The link you can show in the html will be like:

<a href="http://yoursite.com/downloadFile?id=1234">Click here to Download Hello.mp4</a>

在服务器端,您需要一个正在/ downloadFile上的脚本(取决于您的路由),通过id获取文件,并将其作为附件发送给用户。

And in the server side, you need a script which is being called on /downloadFile (depending on your routing), get the file by id and send it to user as an attachment.

<?php

 $fileId = $_POST['id'];

 // so for url http://yoursite.com/downloadFile?id=1234 will download file
 // /pathToVideoFolder/1234.mp4
 $filePath = "/pathToVideoFolder/".$fileId."mp4";
 $fileName = $fileId."mp4"; //or a name from database like getFilenameForID($id)
 //Assume that $filename and $filePath are correclty set.
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Type: application/force-download');
readfile($filePath);

这里Content-Type:application / force-download将强制浏览器显示下载选项无论mime类型的默认设置是什么。

Here 'Content-Type: application/force-download' will force the browser to show the download option no matter what's the default setting is for a mime-type.

无论您的服务器端技术如何,需要注意的是:

No matter what your server side technology is, the headers to look out for are:

'Content-Description: File Transfer'
'Content-Type: application/force-download'
'Content-Disposition: attachment; filename="myfile.mp4"

这篇关于Mp4下载导致浏览器播放文件而不是下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆