欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js如何通过链接下载一个视频(备忘)

程序员文章站 2022-04-21 11:32:53
downloadFile(fileurl, filename) {let _that = this;this.$http.defaults.timeout = 100 * 60 * 1000;this.$http.get(fileurl, {responseType: 'blob',onDownloadProgress(progress) {//_that.downProgress = Math.round(progre....
downloadFile(fileurl, filename) {
				let _that = this;
				this.$http.defaults.timeout = 100 * 60 * 1000;
//fileurl 为视频地址
				this.$http.get(
					fileurl, {
						responseType: 'blob',
						onDownloadProgress(progress) {
							//_that.downProgress = Math.round(progress.loaded / progress.total * 100) + '%'
						}
					}
				).then(response => {
					let blob = response;
					if (typeof window.navigator.msSaveBlob !== 'undefined') {
						window.navigator.msSaveBlob(blob, filename)
					} else {
						let URL = window.URL || window.webkitURL;
						// 使用获取到的blob对象创建的blobUrl
						const blobUrl = URL.createObjectURL(blob);

						const a = document.createElement('a');

						if (typeof a.download === 'undefined') {
							window.location = blobUrl
						} else {
							document.body.appendChild(a)
							a.style.display = 'none'
							a.href = blobUrl;
							// 指定下载的文件名
							a.download = filename;
							a.click();
							document.body.removeChild(a)
							// 移除blob对象的blobUrl
							URL.revokeObjectURL(blobUrl);
						}
					}
					this.downloading = false;
				}).catch((error) => {
					//throw error;
					//this.$message({
					//	showClose: true,
					//	message: '下载失败,请重试..',
					//	type: 'error'
					//});
					//this.downloading = false;
				})
			},

this.$http为通过axios封装的函数  

坑为有跨域问题,需后端配合解决跨域

本文地址:https://blog.csdn.net/q140948940/article/details/107510210

相关标签: javascript url