1. 程式人生 > >ionic3專案版本更新下載

ionic3專案版本更新下載

/**
       * 獲得當前app版本號,如0.01
       * @description  對應/config.xml中version的值
       * @returns {Promise<string>}
       */
    getVersionNumber(): Promise<string> {
        return new Promise((resolve) => {
            this.appVersion.getVersionNumber().then((value: string) => {
                this.currentVersion = value;
                resolve(value);
            }).catch(err => {
                console.log('getVersionNumber:' + err);
            });
        });
    }
alertCtrlMethod() {
        this.alertCtrl.create({
            title: '升級',
            subTitle: '發現新版本,是否立即升級?',
            buttons: [{ text: '取消' },
            {
                text: '確定',
                handler: () => {
                    this.downloadApp();
                }
            }
            ]
        }).present();
    }

    /**
     * 下載安裝app
     */
    downloadApp() {
        if (true) {
            let alert = this.alertCtrl.create({
                title: '下載進度:0%',
                enableBackdropDismiss: false,
                buttons: ['後臺下載']
            });
            alert.present();
            const fileTransfer: FileTransferObject = this.transfer.create();
            const apk = this.file.externalRootDirectory + 'mesnac.apk';
            fileTransfer.download(APK_DOWNLOAD, apk).then(() => {
                this.fileOpener.open(apk, 'application/vnd.android.package-archive');
            });
            fileTransfer.onProgress((event: ProgressEvent) => {
                let num = Math.floor(event.loaded / event.total * 100);
                if (num === 100) {
                    alert.dismiss();
                } else {
                    let title = document.getElementsByClassName('alert-title')[0];
                    title && (title.innerHTML = '下載進度:' + num + '%');
                }
            });
        }
    }

webAPI介面部分:

public class DownloadController : ApiController
    {
        /// <summary>
        /// 實現的方法很簡單,其中就是讀取伺服器的指定路徑檔案流,將其做為返回的HttpResponseMessage的Content
        /// </summary>
        public HttpResponseMessage GetApkFile()
        {
            try
            {
                var FilePath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/apk/android-armv7-debug.apk");
                var stream = new FileStream(FilePath, FileMode.Open);
                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
                response.Content = new StreamContent(stream);
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = "android.apk"
                };
                return response;
            }
            catch
            {
                return new HttpResponseMessage(HttpStatusCode.NoContent);
            }
        }
    }
相關原文連結:https://www.jianshu.com/p/a41223a0b6a2;

                      http://blog.csdn.net/u014742815/article/details/52537093;