前端实现视频或者图片直链下载

简介: 前端实现视频或者图片直链下载

前端实现视频或者图片直链下载


前端通过a标签点击视频直链或者图片直链,会单独打开页面并进行播放视频或者展示图片,但是不会实现直接下载。

为了解决这一问题,我们可以通过以下方法,实现视频或者图片直链下载

downVideo (url, name){
  var xhr = new XMLHttpRequest();
  xhr.open('GET', url, true);
  xhr.responseType = 'arraybuffer';    // 返回类型blob
  xhr.onload = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {
      let blob = this.response;
    // 转换一个blob链接
      let u = window.URL.createObjectURL(new Blob([blob],{ type: 'video/mp4' })) // 视频的type是video/mp4,图片是image/jpeg
    let a = document.createElement('a');
    a.download = name; // 设置下载的文件名
    a.href = u;
    a.style.display = 'none'
    document.body.appendChild(a)
    a.click();
    a.remove();
  }
  };
  xhr.send()
}
//调用  点击按钮实现mp4格式视频下载
downVideo(video.download_url,video.title)

index.html中加上这一句

<meta name=referrer content=no-referrer>

备注:blob 导出 的content-type 类型

1 Response.AddHeader(“content-type”, “application/x-msdownload;”);类型列表
2 “."=“application/octet-stream”
3 “.001”=“application/x-001”
4 “.301”=“application/x-301”
5 “.323”=“text/h323”
6 “.906”=“application/x-906”
7 “.907”=“drawing/907”
8 “.a11”=“application/x-a11”
9 “.acp”=“audio/x-mei-aac”
10 “.ai”=“application/postscript”
11 “.aif”=“audio/aiff”
12 “.aifc”=“audio/aiff”
13 “.aiff”=“audio/aiff”
14 “.anv”=“application/x-anv”
15 “.asa”=“text/asa”
16 “.asf”=“video/x-ms-asf”
17 “.asp”=“text/asp”
18 “.asx”=“video/x-ms-asf”
19 “.au”=“audio/basic”
20 “.avi”=“video/avi”
21 “.awf”=“application/vnd.adobe.workflow”
22 “.biz”=“text/xml”
23 “.bmp”=“application/x-bmp”
24 “.bot”=“application/x-bot”
25 “.c4t”=“application/x-c4t”
26 “.c90”=“application/x-c90”
27 “.cal”=“application/x-cals”
28 “.cat”=“application/vnd.ms-pki.seccat”
29 “.cdf”=“application/x-netcdf”
30 “.cdr”=“application/x-cdr”
31 “.cel”=“application/x-cel”
32 “.cer”=“application/x-x509-ca-cert”
33 “.cg4”=“application/x-g4”
34 “.cgm”=“application/x-cgm”
35 “.cit”=“application/x-cit”
36 “.class”="java/”
37 “.cml”=“text/xml”
38 “.cmp”=“application/x-cmp”
39 “.cmx”=“application/x-cmx”
40 “.cot”=“application/x-cot”
41 “.crl”=“application/pkix-crl”
42 “.crt”=“application/x-x509-ca-cert”
43 “.csi”=“application/x-csi”
44 “.css”=“text/css”
45 “.cut”=“application/x-cut”
46 “.dbf”=“application/x-dbf”
47 “.dbm”=“application/x-dbm”
48 “.dbx”=“application/x-dbx”
49 “.dcd”=“text/xml”
50 “.dcx”=“application/x-dcx”
51 “.der”=“application/x-x509-ca-cert”
52 “.dgn”=“application/x-dgn”
53 “.dib”=“application/x-dib”
54 “.dll”=“application/x-msdownload”
55 “.doc”=“application/msword”
56 “.dot”=“application/msword”
57 “.drw”=“application/x-drw”
58 “.dtd”=“text/xml”
59 “.dwf”=“Model/vnd.dwf”
60 “.dwf”=“application/x-dwf”
61 “.dwg”=“application/x-dwg”
62 “.dxb”=“application/x-dxb”
63 “.dxf”=“application/x-dxf”
64 “.edn”=“application/vnd.adobe.edn”
65 “.emf”=“application/x-emf”
66 “.eml”=“message/rfc822”
67 “.ent”=“text/xml”
68 “.epi”=“application/x-epi”
69 “.eps”=“application/x-ps”
70 “.eps”=“application/postscript”
71 “.etd”=“application/x-ebx”
72 “.exe”=“application/x-msdownload”
73 “.fax”=“image/fax”
74 “.fdf”=“application/vnd.fdf”
75 “.fif”=“application/fractals”
76 “.fo”=“text/xml”
77 “.frm”=“application/x-frm”
78 “.g4”=“application/x-g4”
79 “.gbr”=“application/x-gbr”
80 “.gcd”=“application/x-gcd”
81 “.gif”=“image/gif”
82 “.gl2”=“application/x-gl2”
83 “.gp4”=“application/x-gp4”
84 “.hgl”=“application/x-hgl”
85 “.hmr”=“application/x-hmr”
86 “.hpg”=“application/x-hpgl”
87 “.hpl”=“application/x-hpl”
88 “.hqx”=“application/mac-binhex40”
89 “.hrf”=“application/x-hrf”
90 “.hta”=“application/hta”
91 “.htc”=“text/x-component”
92 “.htm”=“text/html”
93 “.html”=“text/html”
94 “.htt”=“text/webviewhtml”
95 “.htx”=“text/html”
96 “.icb”=“application/x-icb”
97 “.ico”=“image/x-icon”
98 “.ico”=“application/x-ico”
99 “.iff”=“application/x-iff”
100 “.ig4”=“application/x-g4”
101 “.igs”=“application/x-igs”
102 “.iii”=“application/x-iphone”
103 “.img”=“application/x-img”
104 “.ins”=“application/x-internet-signup”
105 “.isp”=“application/x-internet-signup”
106 “.IVF”=“video/x-ivf”
107 “.java”=“java/*”
108 “.jfif”=“image/jpeg”
109 “.jpe”=“image/jpeg”
110 “.jpe”=“application/x-jpe”
111 “.jpeg”=“image/jpeg”
112 “.jpg”=“image/jpeg”
113 “.jpg”=“application/x-jpg”
114 “.js”=“application/x-javascript”
115 “.jsp”=“text/html”
116 “.la1”=“audio/x-liquid-file”
117 “.lar”=“application/x-laplayer-reg”
118 “.latex”=“application/x-latex”
119 “.lavs”=“audio/x-liquid-secure”
120 “.lbm”=“application/x-lbm”
121 “.lmsff”=“audio/x-la-lms”
122 “.ls”=“application/x-javascript”
123 “.ltr”=“application/x-ltr”
124 “.m1v”=“video/x-mpeg”
125 “.m2v”=“video/x-mpeg”
126 “.m3u”=“audio/mpegurl”
127 “.m4e”=“video/mpeg4”
128 “.mac”=“application/x-mac”
129 “.man”=“application/x-troff-man”
130 “.math”=“text/xml”
131 “.mdb”=“application/msaccess”
132 “.mdb”=“application/x-mdb”
133 “.mfp”=“application/x-shockwave-flash”
134 “.mht”=“message/rfc822”
135 “.mhtml”=“message/rfc822”
136 “.mi”=“application/x-mi”
137 “.mid”=“audio/mid”
138 “.midi”=“audio/mid”
139 “.mil”=“application/x-mil”
140 “.mml”=“text/xml”
141 “.mnd”=“audio/x-musicnet-download”
142 “.mns”=“audio/x-musicnet-stream”
143 “.mocha”=“application/x-javascript”
144 “.movie”=“video/x-sgi-movie”
145 “.mp1”=“audio/mp1”
146 “.mp2”=“audio/mp2”
147 “.mp2v”=“video/mpeg”
148 “.mp3”=“audio/mp3”
149 “.mp4”=“video/mpeg4”
150 “.mpa”=“video/x-mpg”
151 “.mpd”=“application/vnd.ms-project”
152 “.mpe”=“video/x-mpeg”
153 “.mpeg”=“video/mpg”
154 “.mpg”=“video/mpg”
155 “.mpga”=“audio/rn-mpeg”
156 “.mpp”=“application/vnd.ms-project”
157 “.mps”=“video/x-mpeg”
158 “.mpt”=“application/vnd.ms-project”
159 “.mpv”=“video/mpg”
160 “.mpv2”=“video/mpeg”
161 “.mpw”=“application/vnd.ms-project”
162 “.mpx”=“application/vnd.ms-project”
163 “.mtx”=“text/xml”
164 “.mxp”=“application/x-mmxp”
165 “.net”=“image/pnetvue”
166 “.nrf”=“application/x-nrf”
167 “.nws”=“message/rfc822”
168 “.odc”=“text/x-ms-odc”
169 “.out”=“application/x-out”
170 “.p10”=“application/pkcs10”
171 “.p12”=“application/x-pkcs12”
172 “.p7b”=“application/x-pkcs7-certificates”
173 “.p7c”=“application/pkcs7-mime”
174 “.p7m”=“application/pkcs7-mime”
175 “.p7r”=“application/x-pkcs7-certreqresp”
176 “.p7s”=“application/pkcs7-signature”
177 “.pc5”=“application/x-pc5”
178 “.pci”=“application/x-pci”
179 “.pcl”=“application/x-pcl”
180 “.pcx”=“application/x-pcx”
181 “.pdf”=“application/pdf”
182 “.pdf”=“application/pdf”
183 “.pdx”=“application/vnd.adobe.pdx”
184 “.pfx”=“application/x-pkcs12”
185 “.pgl”=“application/x-pgl”
186 “.pic”=“application/x-pic”
187 “.pko”=“application/vnd.ms-pki.pko”
188 “.pl”=“application/x-perl”
189 “.plg”=“text/html”
190 “.pls”=“audio/scpls”
191 “.plt”=“application/x-plt”
192 “.png”=“image/png”
193 “.png”=“application/x-png”
194 “.pot”=“application/vnd.ms-powerpoint”
195 “.ppa”=“application/vnd.ms-powerpoint”
196 “.ppm”=“application/x-ppm”
197 “.pps”=“application/vnd.ms-powerpoint”
198 “.ppt”=“application/vnd.ms-powerpoint”
199 “.ppt”=“application/x-ppt”
200 “.pr”=“application/x-pr”
201 “.prf”=“application/pics-rules”
202 “.prn”=“application/x-prn”
203 “.prt”=“application/x-prt”
204 “.ps”=“application/x-ps”
205 “.ps”=“application/postscript”
206 “.ptn”=“application/x-ptn”
207 “.pwz”=“application/vnd.ms-powerpoint”
208 “.r3t”=“text/vnd.rn-realtext3d”
209 “.ra”=“audio/vnd.rn-realaudio”
210 “.ram”=“audio/x-pn-realaudio”
211 “.ras”=“application/x-ras”
212 “.rat”=“application/rat-file”
213 “.rdf”=“text/xml”
214 “.rec”=“application/vnd.rn-recording”
215 “.red”=“application/x-red”
216 “.rgb”=“application/x-rgb”
217 “.rjs”=“application/vnd.rn-realsystem-rjs”
218 “.rjt”=“application/vnd.rn-realsystem-rjt”
219 “.rlc”=“application/x-rlc”
220 “.rle”=“application/x-rle”
221 “.rm”=“application/vnd.rn-realmedia”
222 “.rmf”=“application/vnd.adobe.rmf”
223 “.rmi”=“audio/mid”
224 “.rmj”=“application/vnd.rn-realsystem-rmj”
225 “.rmm”=“audio/x-pn-realaudio”
226 “.rmp”=“application/vnd.rn-rn_music_package”
227 “.rms”=“application/vnd.rn-realmedia-secure”
228 “.rmvb”=“application/vnd.rn-realmedia-vbr”
229 “.rmx”=“application/vnd.rn-realsystem-rmx”
230 “.rnx”=“application/vnd.rn-realplayer”
231 “.rp”=“image/vnd.rn-realpix”
232 “.rpm”=“audio/x-pn-realaudio-plugin”
233 “.rsml”=“application/vnd.rn-rsml”
234 “.rt”=“text/vnd.rn-realtext”
235 “.rtf”=“application/msword”
236 “.rtf”=“application/x-rtf”
237 “.rv”=“video/vnd.rn-realvideo”
238 “.sam”=“application/x-sam”
239 “.sat”=“application/x-sat”
240 “.sdp”=“application/sdp”
241 “.sdw”=“application/x-sdw”
242 “.sit”=“application/x-stuffit”
243 “.slb”=“application/x-slb”
244 “.sld”=“application/x-sld”
245 “.slk”=“drawing/x-slk”
246 “.smi”=“application/smil”
247 “.smil”=“application/smil”
248 “.smk”=“application/x-smk”
249 “.snd”=“audio/basic”
250 “.sol”=“text/plain”
251 “.sor”=“text/plain”
252 “.spc”=“application/x-pkcs7-certificates”
253 “.spl”=“application/futuresplash”
254 “.spp”=“text/xml”
255 “.ssm”=“application/streamingmedia”
256 “.sst”=“application/vnd.ms-pki.certstore”
257 “.stl”=“application/vnd.ms-pki.stl”
258 “.stm”=“text/html”
259 “.sty”=“application/x-sty”
260 “.svg”=“text/xml”
261 “.swf”=“application/x-shockwave-flash”
262 “.tdf”=“application/x-tdf”
263 “.tg4”=“application/x-tg4”
264 “.tga”=“application/x-tga”
265 “.tif”=“image/tiff”
266 “.tif”=“application/x-tif”
267 “.tiff”=“image/tiff”
268 “.tld”=“text/xml”
269 “.top”=“drawing/x-top”
270 “.torrent”=“application/x-bittorrent”
271 “.tsd”=“text/xml”
272 “.txt”=“text/plain”
273 “.uin”=“application/x-icq”
274 “.uls”=“text/iuls”
275 “.vcf”=“text/x-vcard”
276 “.vda”=“application/x-vda”
277 “.vdx”=“application/vnd.visio”
278 “.vml”=“text/xml”
279 “.vpg”=“application/x-vpeg005”
280 “.vsd”=“application/vnd.visio”
281 “.vsd”=“application/x-vsd”
282 “.vss”=“application/vnd.visio”
283 “.vst”=“application/vnd.visio”
284 “.vst”=“application/x-vst”
285 “.vsw”=“application/vnd.visio”
286 “.vsx”=“application/vnd.visio”
287 “.vtx”=“application/vnd.visio”
288 “.vxml”=“text/xml”
289 “.wav”=“audio/wav”
290 “.wax”=“audio/x-ms-wax”
291 “.wb1”=“application/x-wb1”
292 “.wb2”=“application/x-wb2”
293 “.wb3”=“application/x-wb3”
294 “.wbmp”=“image/vnd.wap.wbmp”
295 “.wiz”=“application/msword”
296 “.wk3”=“application/x-wk3”
297 “.wk4”=“application/x-wk4”
298 “.wkq”=“application/x-wkq”
299 “.wks”=“application/x-wks”
300 “.wm”=“video/x-ms-wm”
301 “.wma”=“audio/x-ms-wma”
302 “.wmd”=“application/x-ms-wmd”
303 “.wmf”=“application/x-wmf”
304 “.wml”=“text/vnd.wap.wml”
305 “.wmv”=“video/x-ms-wmv”
306 “.wmx”=“video/x-ms-wmx”
307 “.wmz”=“application/x-ms-wmz”
308 “.wp6”=“application/x-wp6”
309 “.wpd”=“application/x-wpd”
310 “.wpg”=“application/x-wpg”
311 “.wpl”=“application/vnd.ms-wpl”
312 “.wq1”=“application/x-wq1”
313 “.wr1”=“application/x-wr1”
314 “.wri”=“application/x-wri”
315 “.wrk”=“application/x-wrk”
316 “.ws”=“application/x-ws”
317 “.ws2”=“application/x-ws”
318 “.wsc”=“text/scriptlet”
319 “.wsdl”=“text/xml”
320 “.wvx”=“video/x-ms-wvx”
321 “.xdp”=“application/vnd.adobe.xdp”
322 “.xdr”=“text/xml”
323 “.xfd”=“application/vnd.adobe.xfd”
324 “.xfdf”=“application/vnd.adobe.xfdf”
325 “.xhtml”=“text/html”
326 “.xls”=“application/vnd.ms-excel”
327 “.xls”=“application/x-xls”
328 “.xlw”=“application/x-xlw”
329 “.xml”=“text/xml”
330 “.xpl”=“audio/scpls”
331 “.xq”=“text/xml”
332 “.xql”=“text/xml”
333 “.xquery”=“text/xml”
334 “.xsd”=“text/xml”
335 “.xsl”=“text/xml”
336 “.xslt”=“text/xml”
337 “.xwd”=“application/x-xwd”
338 “.x_b”=“application/x-x_b”
339 “.x_t”=“application/x-x_t”

文章参考:

https://blog.csdn.net/baidu_41457482/article/details/117106614

https://blog.csdn.net/antuo7251/article/details/101683763?spm=1001.2014.3001.5501

目录
相关文章
|
2月前
|
存储 前端开发 JavaScript
🚀前端轻松实现网页内容转换:一键复制、保存图片及生成 Markdown
在现代前端开发中,提升用户的交互体验至关重要。本文将详细介绍如何使用 HTML2Canvas 和 Turndown 两个强大的 JavaScript 库,实现将网页选中文本转化为图片并保存或复制到剪贴板,或将内容转换为 Markdown 格式。文章包含核心代码实现、技术细节和功能拓展方向,为开发者提供了一个轻量级的解决方案,提升用户体验。
170 68
|
1月前
|
前端开发 API
前端界面生成PDF并导出下载
【10月更文挑战第21天】利用合适的第三方库,你可以在前端轻松实现界面生成 PDF 并导出下载的功能,为用户提供更方便的文档分享和保存方式。你还可以根据具体的需求进一步优化和定制生成的 PDF 文件,以满足不同的业务场景要求。
|
2月前
|
JavaScript 前端开发 Python
django接收前端vue传输的formData图片数据
django接收前端vue传输的formData图片数据
60 4
|
2月前
|
前端开发 小程序 Java
java基础:map遍历使用;java使用 Patten 和Matches 进行正则匹配;后端传到前端展示图片三种情况,并保存到手机
这篇文章介绍了Java中Map的遍历方法、使用Pattern和matches进行正则表达式匹配,以及后端向前端传输图片并保存到手机的三种情况。
25 1
|
2月前
|
前端开发 JavaScript 编译器
不走弯路,纯前端如何把图片导出成视频!
【10月更文挑战第3天】不走弯路,纯前端如何把图片导出成视频!
62 3
|
2月前
|
JavaScript 前端开发 编译器
吐血整理:纯前端如何实现批量dom转图片,并下载成压缩包
【10月更文挑战第2天】吐血整理:纯前端如何实现批量dom转图片,并下载成压缩包
59 2
|
2月前
|
资源调度 前端开发 安全
前端实战:基于Verdaccio搭建私有npm仓库,轻松上传与下载自定义npm插件包
前端实战:基于Verdaccio搭建私有npm仓库,轻松上传与下载自定义npm插件包
125 0
|
2月前
|
存储 人工智能 前端开发
前端大模型应用笔记(三):Vue3+Antdv+transformers+本地模型实现浏览器端侧增强搜索
本文介绍了一个纯前端实现的增强列表搜索应用,通过使用Transformer模型,实现了更智能的搜索功能,如使用“番茄”可以搜索到“西红柿”。项目基于Vue3和Ant Design Vue,使用了Xenova的bge-base-zh-v1.5模型。文章详细介绍了从环境搭建、数据准备到具体实现的全过程,并展示了实际效果和待改进点。
172 2
|
2月前
|
JavaScript 前端开发 程序员
前端学习笔记——node.js
前端学习笔记——node.js
46 0
|
2月前
|
人工智能 自然语言处理 运维
前端大模型应用笔记(一):两个指令反过来说大模型就理解不了啦?或许该让第三者插足啦 -通过引入中间LLM预处理用户输入以提高多任务处理能力
本文探讨了在多任务处理场景下,自然语言指令解析的困境及解决方案。通过增加一个LLM解析层,将复杂的指令拆解为多个明确的步骤,明确操作类型与对象识别,处理任务依赖关系,并将自然语言转化为具体的工具命令,从而提高指令解析的准确性和执行效率。