npm i --save @triniwiz/nativescript-downloader
- 版本:3.1.0
- GitHub: https://github.com/triniwiz/plugins
- NPM: https://npmjs.net.cn/package/%40triniwiz%2Fnativescript-downloader
- 下载量
- 昨天:15
- 上周:101
- 上个月:299
Nativescript 下载器
安装
npm install @triniwiz/nativescript-downloader
使用方法
TypeScript
import { Downloader } from '@triniwiz/nativescript-downloader';
Downloader.setTimeout(120); //Increase timeout default 60s
import { Downloader, ProgressEventData, DownloadEventData } from 'nativescript-downloader';
const downloader = new Downloader();
const imageDownloaderId = Downloader.createDownload({
url:
'https://wallpaperscraft.com/image/hulk_wolverine_x_men_marvel_comics_art_99032_3840x2400.jpg'
});
downloader
.start(imageDownloaderId, (progressData: ProgressEventData) => {
console.log(`Progress : ${progressData.value}%`);
console.log(`Current Size : ${progressData.currentSize}%`);
console.log(`Total Size : ${progressData.totalSize}%`);
console.log(`Download Speed in bytes : ${progressData.speed}%`);
})
.then((completed: DownloadEventData) => {
console.log(`Image : ${completed.path}`);
})
.catch(error => {
console.log(error.message);
});
JavaScript
var Downloader = require('@triniwiz/nativescript-downloader').Downloader;
Downloader.setTimeout(120); //Increase timeout default 60s
var Downloader = require('@triniwiz/nativescript-downloader').Downloader;
var downloader = new Downloader();
var imageDownloaderId = downloadManager.createDownload({
url:
'https://wallpaperscraft.com/image/hulk_wolverine_x_men_marvel_comics_art_99032_3840x2400.jpg'
});
downloader
.start(imageDownloaderId, progressData => {
console.log(`Progress : ${progressData.value}%`);
})
.then(completed => {
console.log(`Image : ${completed.path}`);
})
.catch(error => {
console.log(error.message);
});
API
方法 | 默认 | 类型 | 描述 |
---|---|---|---|
createDownload(options: DownloadOptions) | string |
创建一个下载任务,返回任务的ID | |
getStatus(id: string) | StatusCode |
获取下载任务的状态。 | |
start(id: string, progress?: Function) | Promise |
开始下载任务。 | |
resume(id: string) | void |
恢复下载任务。 | |
cancel(id: string) | void |
Cancels a download task. | |
pause(id: string) | void |
暂停下载任务。 | |
getPath(id: string) | void |
返回下载任务的路径。 |
示例图片
IOS | Android |
---|---|
Angular
自定义下载器选项
// Request format for Downlaoder
DownloadOptions {
url: string;
query?: Object | string;
headers?: Object;
path?: string;
fileName?: string;
}
可在任何地方使用的服务文件
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { Downloader, ProgressEventData, DownloadEventData } from '@triniwiz/nativescript-downloader';
@Injectable({
providedIn: 'root',
})
export class DataDownLoadService {
public database: any;
downloader: Downloader = new Downloader();
constructor() { }
/**
* @ngdoc method
* @name downloadDb
* @description download generic method for nay file
* @memberof DataDownLoadService
* @param apiUrl : - https://myserver.com/mypath
* @param filename :- myfile.zip ...
* @param downloadlocation : mobile local location
*/
downloadFile(apiUrl, filename, downloadlocation) {
const subject = new Subject<any>();
// Request format for Downlaoder
// DownloadOptions {
// url: string;
// query?: Object | string;
// headers?: Object;
// path?: string;
// fileName?: string;
// }
// Prepare the header with token work
const headerHttp = {
"Content-Type": "application/json",
"Authorization": 'Bearer ' + 'Token...'
}
const url = apiUrl;
const zipDownloaderId = this.downloader.createDownload({
url: url,
headers: headerHttp,
path: downloadlocation,
fileName: filename
});
this.downloader
.start(zipDownloaderId, (progressData: ProgressEventData) => {
console.log(`Progress : ${progressData.value}%`);
console.log(`Current Size : ${progressData.currentSize}%`);
console.log(`Total Size : ${progressData.totalSize}%`);
console.log(`Download Speed in bytes : ${progressData.speed}%`);
})
.then((completed: DownloadEventData) => {
console.log(`zip file saved at : ${completed.path}`);
subject.next(true);
})
.catch(error => {
console.log('downloadDb', error.message);
subject.error(error);
});
return subject.asObservable();
}
}
待办事项
- [ ] 本地通知
Apache License 版本 2.0