是否有可能使用下载属性强制下载? [英] Is it possible to force download using download attribute?

查看:91
本文介绍了是否有可能使用下载属性强制下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用下载属性来强制下载,但我想做一些不同的事情。

I know how to use download attribute to force download, but I want to do something different.

<a href="filename.mp4" download=""><img src="download.png"></a>

这是下载属性,可正常运作,但我想要使用 meta 标签下载,如下所示:

This is the download attribute which works properly, but I want to download using the meta tag like so:

<meta http-equiv="refresh" content="5; url=uploads/<?php echo $_POST["filename"]; ?>"> Please wait while your file start downloading.

它可以很好地处理zipfile,rar等扩展。但是当我尝试使用mp4 ,它会在浏览器中打开文件,而不是强制下载。

It is working great with extensions like zipfile, rar, etc.. But when I try to use mp4, it opens up the file in a browser instead of force download.

推荐答案

我改变了一些东西。先生,我认为它可以帮助你:
第一页代码:

I change some thing. Sir i think it may help you: The first page code:

<?php $file = $_POST["filename"]; ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta http-equiv="refresh" content="5; url=hello.php?file=<?php echo $file; ?>">

    <title>Document</title>
</head>
<body>

</body>
</html>

然后我创建一个hello.php文件,它包含:

Then i create a hello.php file and it contain:

<?php
$file = 'uploads/' . $_GET['file'];

if( !file_exists($file) ) die("File not found");

header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header("Content-Length: " . filesize($file));
header('Content-type: vedio/mp4');

readfile($file);
?>

该过程自动下载文件。

这篇关于是否有可能使用下载属性强制下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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