2017-08-22 40 views
5

我正在面臨ionic3應用程序中遇到的問題。離子文件傳輸插件不能在生產版本中工作

讓我詳細描述我的情況: 其實我需要離線支持離線應用程序。所以每次我調用API時,我都會將數據存儲到本地存儲中。並從api下載圖像到我的本地目錄。這樣當本地資源不可用時,我可以獲取數據和圖像。

我使用這個插件來從服務器下載圖像局部: https://ionicframework.com/docs/native/file-transfer/

這是工作的罰款,如果我運行下面的命令:

ionic cordova run android 

但它不工作,當我運行下面的命令:

ionic cordova run android --prod 

代碼:

import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer'; 
import { File } from '@ionic-native/file'; 

constructor(private transfer: FileTransfer, private file: File) { } 

const fileTransfer: FileTransferObject = this.transfer.create(); 

download() { 
    const url = 'http://www.example.com/file.pdf'; 
    fileTransfer.download(url, this.file.dataDirectory + 'file.pdf').then((entry) => { 
    console.log('download complete: ' + entry.toURL()); 
    }, (error) => { 
    // handle error 
    }); 
} 

我沒有從控制檯收到任何錯誤或問題。所以我不知道我在想什麼。還有配置好本地存儲的權限。所以權限不是問題。

感謝您的時間和回覆。

+0

你如何告訴它不工作? – Sampath

+0

@Sampath它沒有顯示任何錯誤,也沒有開始下載。 –

+0

@HiteshUpadhyay對不起,我找不到任何解決方案,但可能我們可以使用類似保護的東西來防止混淆生產代碼。 – Mohsen

回答

5

最後我找到了解決這個問題的方法! 在第一,你應該更新這個命令:

npm i @ionic/[email protected] --save 
npm i [email protected] --save 

,可能在你的代碼的某個地方,你叫什麼文件傳輸插件之前

platform.ready.then()

在我的情況相關:我注入一些服務包括這樣一行:

this.fileTransfer = this.transfer.create();

我將其更改爲:

this.platform.ready().then(() => { 
    // Okay, so the platform is ready and our plugins are available. 
    // Here you can do any higher level native things you might need. 
    this.fileTransfer = this.transfer.create(); 
}); 

現在一切正常。

更多詳細信息:

爲什麼這一工作在調試模式?

答案很清楚,因爲在調試模式下設備準備好的事件給這個絕對的火災和文件傳輸很長時間!但在生產模式下,設備準備就緒非常快,在此之前調用文件傳輸。我希望這對你有所幫助。

+0

非常感謝你,它幫助了我很多:) –