当前位置: 移动技术网 > IT编程>开发语言>JavaScript > js如何通过链接下载一个视频(备忘)

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

2020年07月27日  | 移动技术网IT编程  | 我要评论
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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网