视频模型内容页mp4视频下载按钮实现
来源:本站原创
点击数: 次
发布时间:2026年03月26日
问题描述:
在视频模型的内容页,视频播放器下面增加视频下载按钮。浏览器访问mp4地址默认是在线播放,通过适当的调整实现视频下载,既不影响当前视频播放器播放的前提
问题解决:
一、Nginx 实现
1,通过虚拟目录方式(推荐)
配置
server {
listen 80;
server_name yourdomain.com;
# 播放路径(不变)
location /upload/ {
root /data/www; # 实际路径:/data/www/upload/
}
# 下载路径(关键)
location /download/ {
alias /data/www/upload/;
add_header Content-Disposition 'attachment';
add_header Content-Type application/octet-stream;
}
}
前端使用
<!-- 播放 -->
<video controls width="600">
<source src="/upload/main/contentmanage/video/2026/03/30/22.mp4" type="video/mp4">
</video>
<!-- 下载 -->
<a href="/download/main/contentmanage/video/2026/03/30/22.mp4">
下载视频
</a>
2,通过参数控制方式
配置
location /upload/ {
root /data/www;
# 如果带 ?download=1 才触发下载
if ($arg_download = "1") {
add_header Content-Disposition 'attachment';
add_header Content-Type application/octet-stream;
}
}
前端
<a href="/upload/main/contentmanage/video/2026/03/30/22.mp4?download=1">
下载视频
</a>
⚠️ Nginx注意
alias和root不要混用错误if虽然可用,但复杂场景建议用map优化(这里简单用法OK)
二、IIS 实现
1,通过虚拟目录方式(最推荐)
步骤
① 添加虚拟目录
| 名称 | 物理路径 |
|---|---|
| upload | D:\www\upload |
| download | D:\www\upload |
② 在 /download 目录放 web.config
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Content-Disposition" value="attachment" />
<add name="Content-Type" value="application/octet-stream" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
前端
<!-- 播放 -->
<video controls width="600">
<source src="/upload/main/contentmanage/video/2026/03/30/22.mp4" type="video/mp4">
</video>
<!-- 下载 -->
<a href="/download/main/contentmanage/video/2026/03/30/22.mp4">
下载视频
</a>
2,通过参数控制方式(URL Rewrite)
前提
需要安装:IIS URL Rewrite 模块
web.config 配置
<configuration>
<system.webServer>
<rewrite>
<outboundRules>
<rule name="Download MP4 by Query">
<match serverVariable="RESPONSE_Content-Disposition" pattern=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="download=1" />
</conditions>
<action type="Rewrite" value="attachment" />
</rule>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>
前端
<a href="/upload/main/contentmanage/video/2026/03/30/22.mp4?download=1">
下载视频
</a>
