UNPKG

gisq-hb-file-lib

Version:

```

146 lines (119 loc) 4.83 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.gisqFileUtil = {})); })(this, (function (exports) { 'use strict'; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } var GisqDownLoadHbLib = function () { function downLoad(options) { var options = options || {}; this.path = options.path; this.method = options.method || "GET"; this.options = options; this.dtask = null; this.init(); } downLoad.prototype.init = function () { if (this.dtask) { console.log("下载任务已创建!"); return; } console.log("ddd" + (typeof plus === "undefined" ? "undefined" : _typeof(plus))); if (typeof plus == "undefined") return; var _this = this; this.dtask = plus.downloader.createDownload(this.path, this.options, function (d, status) { if (status == 200) { //下载成功 //console.log('绝对路径:'+d.filename); //这里传入的是相对路径,方便缓存显示,回调过去的是相对路径 if (this.options.complete) this.options.complete(d.filename); } else { //下载失败,需删除本地临时文件,否则下次进来时会检查到图片已存在 //console.log("下载失败=" + status + "==" + relativePath); //dtask.abort();//文档描述:取消下载,删除临时文件;(但经测试临时文件没有删除,故使用delFile()方法删除); /* if (relativePath != null) { FileUtil.delFile(relativePath); } */ if (this.options.error) this.options.error(originalUrl); } }); this.dtask.addEventListener("statechanged", function (task, status) { if (!_this.dtask) return; console.log(task); switch (task.state) { case 1: // 开始 //outLine( "开始下载..." ); if (_this.options.start) _this.options.start(); break; case 2: // 已连接到服务器 //outLine( "链接到服务器..." ); if (_this.options.connected) _this.options.connected(); break; case 3: // 已接收到数据 //outSet( "下载数据更新:" ); //outLine( task.downloadedSize+"/"+task.totalSize ); if (_this.options.downloading) _this.options.downloading(task.downloadedSize, task.totalSize); break; case 4: // 下载完成 //outSet( "下载完成!" ); //outLine( task.totalSize ); if (_this.options.finish) _this.options.finish(task.downloadedSize, task.totalSize); break; } }); }; downLoad.prototype.startDownloadTask = function () { if (!this.dtask) { //errorCallback("请先创建下载任务!"); throw "请先创建下载任务!"; } this.dtask.start(); }; downLoad.prototype.pauseDownloadTask = function () { if (!this.dtask) { //errorCallback("请先创建下载任务!"); throw "请先创建下载任务!"; } dtask.pause(); }; downLoad.prototype.resumeDownloadTask = function () { if (!this.dtask) { //errorCallback("请先创建下载任务!"); throw "请先创建下载任务!"; } dtask.resume(); }; downLoad.prototype.cancelDownloadTask = function () { if (!this.dtask) { //errorCallback("请先创建下载任务!"); throw "请先创建下载任务!"; } dtask.abort(); }; downLoad.prototype.readLocalFileHbUrl = function (hbUrl, successCallback, errorCallback) { plus.io.resolveLocalFileSystemURL(hbUrl, function (fs) { // 可通过fs进行文件操作 if (successCallback) successCallback(fs); }, function (e) { if (errorCallback) errorCallback(e); }); }; downLoad.prototype.convertLocalFileFormHbUrl = function (hbUrl, successCallback, errorCallback) { return plus.io.convertLocalFileSystemURL(hbUrl); }; return downLoad; }(); exports.gisqDownLoadHbLib = GisqDownLoadHbLib; Object.defineProperty(exports, '__esModule', { value: true }); }));