Mp4下载导致浏览器播放文件而不是下载

时间:2014-01-13 12:50:16

标签: google-chrome browser download streaming

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

如何强制浏览器下载文件。

问候并感谢 克雷格

5 个答案:

答案 0 :(得分:12)

您必须使用HTTP标头“内容处理”和“内容类型:应用程序/强制下载”,这将强制浏览器下载内容在那里显示它。

根据您所使用的服务器端语言,实现方式有所不同。在

的情况下

PHP:

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

将为您完成这项工作。

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

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

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

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

<?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-type的默认设置是什么。

无论您的服务器端技术是什么,要注意的标题是:

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

答案 1 :(得分:7)

听起来你正在使用直接href到mp4。如果您在网站上使用任何服务器端语言(ieasp.net,php等)语言,则可以强制下载。在asp或.net中,你可以使用带有“content-disposition”,“attachment; filename = fname.ext”的HttpHandlers。  或者在MVC中返回File()ActionResult。如果您可以使用任何服务器端代码,请告诉我,我可以提供一些代码。

或者,您可以尝试使用html5下载属性:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a?redirectlocale=en-US&redirectslug=HTML%2FElement%2Fa#attr-download

即。 <a href="http://www.w3schools.com/images/myw3schoolsimage.jpg" download="downloadfilename">

或者,尝试javascript / jQuery。这是一个插件:http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/

答案 2 :(得分:1)

设置Content-Disposition标头应该修复它。

Content-Disposition: attachment; filename=whatever.mp4;

在服务器设置或页面预处理中。

答案 3 :(得分:0)

如果您需要跨浏览器解决方案

您需要服务器端代码才能下载文件

例如: 我正在研究jsp技术,如果你可以在你的网站上使用jsp,你可以在文件download.jsp中尝试以下代码:

<%@ page import="java.io.*, java.lang.*, java.util.*" %>
<%
String filename=request.getParameter("filename");   
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition",
"attachment;filename="+filename);
 %>

<%
/*
 File file = new File(filepath+filename );*/
 String path = getServletContext().getRealPath("/mp4/"+filename); 
 File file = new File(path);
FileInputStream fileIn = new FileInputStream(file);
ServletOutputStream out1 = response.getOutputStream();

byte[] outputByte = new byte[4096];
//copy binary contect to output stream
while(fileIn.read(outputByte, 0, 4096) != -1)
{
    out1.write(outputByte, 0, 4096);
}
fileIn.close();
out1.flush();
out1.close();
 %>

您可以将上面的代码放在一个文件中:download.jsp

然后在您的页面链接中,您将使用它:

<a href="download.jsp?filename=song1.mp4">song1</a>

向你致以最良好的祝愿

答案 4 :(得分:0)

你可以通过几种方式完成它。我不确定您使用IIS或Apache以及您使用的服务器端语言,但所有技术都类似。

您可以将MIME类型application / octect-stream添加到IIS或apache中的扩展名.mp4,以便在下载提示符下显示扩展名为.mp4的所有文件。这是显示“下载”提示的最简单,最确定的火灾方式。

Plz见下面的例子。

http://www.codingstaff.com/learning-center/other/how-to-add-mime-types-to-your-server

在上面的示例中,不是设置video / mp4 fpr .mp4扩展名,而是将其更改为application / octect-stream

此外,同样可以通过服务器端代码完成(PHP代码)。代码也与ASP.NET类似,请谷歌“强制文件下载”

$file_url = 'http://www.myremoteserver.com/file.mp4';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\""); 
readfile($file_url);
相关问题