import RNFetchBlob, { StatefulPromise, FetchBlobResponse } from 'react-native-fetch-blob'; export type IhttpMethods = "POST" | "GET" | "DELETE" | "PUT" | "post" | "get" | "delete" | "put"; import { IdownloadFile } from "../aim/face" export default class { /** * put上传文件 * * @static * @param {IhttpMethods} method * @param {string} url * @param {{ [key: string]: string }} [headers] * @param {string} [sFilePath] * @returns {StatefulPromise} */ static putFile(method: IhttpMethods, url: string, headers?: { [key: string]: string }, sFilePath?: string): StatefulPromise { return RNFetchBlob.fetch(method, url, headers, RNFetchBlob.wrap(sFilePath)); } static readFile(sFilePath) { return RNFetchBlob.fs.readFile(this.upDocumentFilePath(sFilePath), "utf8"); } /** * 获取document文件夹路径下的文件 * * @static * @param {any} sFilePath * @returns */ static upDocumentFilePath(sFilePath) { return RNFetchBlob.fs.dirs.DocumentDir + '/' + sFilePath; } /** * get下载文件 * * @static * @param {string} sUrl * @param {string} sLocalPath * @returns {StatefulPromise} */ static getFile(sUrl: string, sLocalPath: string): StatefulPromise { return RNFetchBlob .config({ path: sLocalPath }) .fetch('GET', sUrl) } static installApp(oDownload: IdownloadFile) { let android = RNFetchBlob.android let dirs = RNFetchBlob.fs.dirs console.warn(oDownload.file); RNFetchBlob .config({ // response data will be saved to this path if it has access right. path: dirs.DownloadDir, // by adding this option, the temp files will have a file extension appendExt : 'apk' }) .fetch('GET', oDownload.file ) // listen to download progress event, every 10% .progress((received, total) => { console.warn('progress', received / total) }) .then((res) => { android.actionViewIntent(res.path(), 'application/vnd.android.package-archive') }) .catch((err) => { }) } static installApp2(oDownload: IdownloadFile) { const android = RNFetchBlob.android RNFetchBlob.config({ addAndroidDownloads: { useDownloadManager: true, title: oDownload.title, description: oDownload.desc, mime: 'application/vnd.android.package-archive', mediaScannable: true, notification: true, } }) .fetch('GET', oDownload.file) .then((res) => { android.actionViewIntent(res.path(), 'application/vnd.android.package-archive') }) } }