Skip to content

文件下载模块

数据结构

ESDownloadInfo

下载信息

参数描述类型非空
id下载的idnumber
fileName文件名string
fileMD5文件MD5值string
fileUrl文件下载地址string
fileType文件类型string
fileLength文件长度number
downloadLength文件下载的长度number
params下载文件的参数ESDownloadParams
filePath文件存储目录string

ESDownloadParams

下载请求参数

参数描述类型非空
header下载请求header参数number

ESDownloadStatus

下载状态

参数描述类型非空
state下载状态ESDownloadState
download下载信息ESDownloadInfo
code下载状态码number
message下载信息string
downloadSize下载文件长度number
totalSize下载文件总长度number

ESDownloadState

下载状态值

参数描述类型非空
ES_DOWNLOAD_STATE_INIT下载初始化状态number
ES_DOWNLOAD_STATE_START下载开始状态number
ES_DOWNLOAD_STATE_PROGRESS下载进度状态number
ES_DOWNLOAD_STATE_STOP下载停止状态number
ES_DOWNLOAD_STATE_CANCEL下载取消状态number
ES_DOWNLOAD_STATE_SUCCESS下载成功状态number
ES_DOWNLOAD_STATE_ERROR下载错误状态number

接口

initDownload

该方法初始化默认下载设置。

完整方法声明:function initDownload(): void

  • 参数:

  • 返回值:
属性描述类型默认值
void

initDownloadPath

该方法初始化下载设置。

完整方法声明:function initDownloadPath(downloadCacheDir: string, interpolator: number): void

  • 参数:
参数描述类型非空
downloadCacheDir下载文件缓存的目录string
interpolator下载进度回调频率number
  • 返回值:
属性描述类型默认值
void

download

该方法用于下载。

完整方法声明:function download(download: ESDownloadInfo): void

  • 参数:
参数描述类型非空
download下载信息ESDownloadInfo
  • 返回值:
属性描述类型默认值
void

start

该方法用于开始下载。

完整方法声明:function start(download: ESDownloadInfo): void

  • 参数:
参数描述类型非空
download下载信息ESDownloadInfo
  • 返回值:
属性描述类型默认值
void

stop

该方法用于停止下载。

完整方法声明:function stop(download: ESDownloadInfo): void

  • 参数:
参数描述类型非空
download下载信息ESDownloadInfo
  • 返回值:
属性描述类型默认值
void

cancel

该方法用于取消下载。

完整方法声明:function cancel(download: ESDownloadInfo): void

  • 参数:
参数描述类型非空
download下载信息ESDownloadInfo
  • 返回值:
属性描述类型默认值
void

release

该方法用于回收资源。

完整方法声明:function release(): void

  • 参数:

  • 返回值:
属性描述类型默认值
void

addListener

该方法用于添加监听。

完整方法声明:function addListener(download: ESDownloadInfo, listener: ESDownloadListener): void

  • 参数:
参数描述类型非空
download下载信息ESDownloadInfo
listener下载监听ESDownloadListener
  • 返回值:
属性描述类型默认值
void

removeListener

该方法用于删除监听。

完整方法声明:function removeListener(listener: ESDownloadListener): void

  • 参数:
参数描述类型非空
listener下载监听ESDownloadListener
  • 返回值:
属性描述类型默认值
void

基础用法

  • 代码示例:
    点击查看源码
    vue
    <template>
    	<div class='es-sdk-root-css'>
    		<s-title-view class='es-sdk-content-title-css' :text='this.$options.name' />
    		<div class='es-sdk-content-divider-css' />
    		<div class='es-sdk-content-column-css'>
    			<div class='es-sdk-content-row-css'>
    				<s-text-view text='下载状态:' />
    				<s-text-view :text='downloadStatus' />
    				<s-text-view text='|' />
    				<s-text-view text='文件总长度:' />
    				<s-text-view :text="totalSizeRef + ''" />
    				<s-text-view text='|' />
    				<s-text-view text='下载文件长度:' />
    				<s-text-view :text="downloadSizeRef + '' " />
    			</div>
    			<div class='es-sdk-content-column-css'>
    				<div class='es-sdk-content-row-css'>
    					<s-text-button text='初始化下载' @onButtonClicked='initDownloadModule' />
    					<s-text-button text='下载' @onButtonClicked='downloadFile' />
    					<s-text-button text='开始' @onButtonClicked='startDownload' />
    					<s-text-button text='停止' @onButtonClicked='stopDownload' />
    					<s-text-button text='取消' @onButtonClicked='cancelDownload' />
    				</div>
    			</div>
    			<div class='es-sdk-content-column-css'>
    				<div class='es-sdk-content-row-css'>
    					<s-text-button text='初始化自定义路径' @onButtonClicked='initDownloadPath' />
    					<s-text-button text='下载' @onButtonClicked='downloadFile2' />
    					<s-text-button text='开始' @onButtonClicked='startDownload2' />
    					<s-text-button text='停止' @onButtonClicked='stopDownload2' />
    					<s-text-button text='取消' @onButtonClicked='cancelDownload2' />
    				</div>
    			</div>
    		</div>
    	</div>
    </template>
    
    <script lang='ts'>
    
    import {defineComponent} from '@vue/runtime-core';
    import {ref} from 'vue';
    import {
    	ESDownload,
    	ESDownloadInfo,
    	ESDownloadListener,
    	useESDownload,
    } from '@extscreen/es3-core';
    
    export default defineComponent({
    	name: '下载模块',
    	setup() {
    
    		const download: ESDownload = useESDownload();
    		const downloadStatus = ref('');
    		const totalSizeRef = ref(0);
    		const downloadSizeRef = ref(0);
    
    		const listener: ESDownloadListener = {
    
    			onDownloadInit(download: ESDownloadInfo) {
    				console.log('------onDownloadInit----->>>', download);
    				downloadStatus.value = '下载初始化';
    			},
    
    			onDownloadStart(download: ESDownloadInfo) {
    				console.log('------onDownloadStart----->>>', download);
    				downloadStatus.value = '下载开始';
    			},
    
    			onDownloadStop(download: ESDownloadInfo) {
    				console.log('------onDownloadStop----->>>', download);
    				downloadStatus.value = '下载停止';
    			},
    
    			onDownloadCancel(download: ESDownloadInfo) {
    				console.log('------onDownloadCancel----->>>', download);
    				downloadStatus.value = '下载取消';
    			},
    
    			onDownloadSuccess(download: ESDownloadInfo) {
    				console.log('------onDownloadSuccess----->>>', download);
    				downloadStatus.value = '下载成功';
    			},
    
    			onDownloadError(download: ESDownloadInfo) {
    				console.log('------onDownloadError----->>>', download);
    				downloadStatus.value = '下载失败';
    			},
    
    			onDownloadProgress(download: ESDownloadInfo, downloadSize: number, totalSize: number) {
    				console.log('------onDownloadProgress----->>>', download);
    				downloadStatus.value = '正在下载';
    				totalSizeRef.value = totalSize;
    				downloadSizeRef.value = downloadSize;
    			},
    		};
    
    		const d1: ESDownloadInfo = {
    			id: 1,
    			fileUrl: 'http://qcloudcdn.a311.ottcn.com/data_center/videos/SHORT/DEFAULT/2023/08/25/7d3623ae-c002-4929-b5a2-fe10bca06bfc.mp4',
    			fileMD5: '5e236d028a40cc8e1ec24697dee869a3',
    			fileName: 'aaaa.mp4',
    		};
    
    		const d2: ESDownloadInfo = {
    			id: 2,
    			fileUrl: 'http://qcloudcdn.a311.ottcn.com/data_center/videos/SHORT/DEFAULT/2023/09/07/a2d3da6d-469e-4f99-a2d0-c001741003f8.mp4',
    			fileMD5: '8d7b49d28365bad9728ba0490b0d5014',
    			fileName: 'bbbbbb.mp4',
    		};
    
    		function onESCreate(params) {
    			download.addListener(d2, listener);
    		}
    
    		function onESDestroy() {
    			download.removeListener(listener);
    		}
    
    		function initDownloadModule() {
    			console.log('------initDownloadModule----->>>');
    			download.initDownload();
    		}
    
    		function downloadFile() {
    			console.log('------downloadFile----->>>');
    			download.download(d2);
    		}
    
    		function startDownload() {
    			console.log('------startDownload----->>>');
    			download.start(d2);
    		}
    
    		function stopDownload() {
    			console.log('------stopDownload----->>>');
    			download.stop(d2);
    		}
    
    		function cancelDownload() {
    			console.log('------cancelDownload----->>>');
    			download.cancel(d2);
    		}
    
    		//-------------------------------------------------
    		function initDownloadPath() {
    			console.log('------initDownloadModulePath----->>>');
    			download.init('/vvvvideo/cache', 1000);
    		}
    
    		function downloadFile2() {
    			console.log('------downloadFile----->>>');
    			download.download(d2);
    		}
    
    		function startDownload2() {
    			console.log('------startDownload----->>>');
    			download.start(d2);
    		}
    
    		function stopDownload2() {
    			console.log('------stopDownload----->>>');
    			download.stop(d2);
    		}
    
    		function cancelDownload2() {
    			console.log('------cancelDownload----->>>');
    			download.cancel(d2);
    		}
    
    		return {
    			downloadStatus,
    			totalSizeRef,
    			downloadSizeRef,
    			initDownloadModule,
    			downloadFile,
    			startDownload,
    			stopDownload,
    			cancelDownload,
    			initDownloadPath,
    			downloadFile2,
    			startDownload2,
    			stopDownload2,
    			cancelDownload2,
    			onESCreate,
    			onESDestroy,
    		};
    	},
    });
    
    </script>
    <style>
    </style>