2017-02-10 137 views
2

我期待從我的離子應用程序上的互聯網/服務器上下載文件。我一直在使用文件傳輸插件來嘗試實現相同的功能,但文件沒有被下載。無法使用FileTransfer下載Cordova和Ionic中的文件

我從插件獲得成功回調,但如果我嘗試從fileOpener2插件打開文件,則會出現該文件不存在的錯誤。我檢查了所有手機的存儲空間,但找不到。 PFB的代碼我使用:

var fileTransfer = new FileTransfer(); 

var imguri = "http://cdn.wall-pix.net/albums/art-space/00030109.jpg"; 
var targetPath = cordova.file.dataDirectory + "testImage.jpg"; 
fileTransfer.download(
imguri, 
targetPath, 
function(entry) { 
    console.log("download complete - Internal URL: " + entry.toInternalURL()); 
    console.log("download complete - Full Path: " + entry.fullPath); 

          $cordovaFileOpener2.open(entry.fullPath, 'image/jpeg').then(function() { 
         // file opened successfully 
         console.log("File opened successfully!"); 
        }, function(err) { 
         // An error occurred. Show a message to the user 
         console.log(JSON.stringify(err)); 
        }); 



}, 
function(error) { 
    console.log("download error source " + error.source); 
    console.log("download error target " + error.target); 
    console.log("download error code" + error.code); 
}, 
false, 
{ 
    headers: { 
     "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA==" 
    } 
} 
); 

在$ cordovaFileOpener2.open,如果我給entry.toInternalURL作爲參數,我得到這個錯誤 - >無法找到配置的根含有/data/data/com.echidna。 foodreview/files/testImage.jpg

在$ cordovaFileOpener2.open中,如果我給entry.fullPath作爲參數,我得到這個錯誤 - > {「status」:9,「message」:「File not found」}

從我的文件管理器中,我搜索了testImage.jpg文件並找不到文件。這應該意味着文件並未首先下載,那麼成功回調是如何被觸發的?

回答

1

此問題似乎是Android 6.0及更高版本中應用所需的新用戶權限。文件插件不要求用戶訪問存儲。這必須首先由應用程序請求,並可以由另一個插件處理。有幾個插件,但我使用https://github.com/NeoLSN/cordova-plugin-android-permission,這對我很有用!

因此,在您下載之前,請檢查您是否有存儲寫入權限。如果你不這樣做,那麼請求權限(檢查和請求權限由上面鏈接中的插件處理)。獲得訪問權限後,下載任何你想要的文件!

乾杯!

0

,而不是

file.dataDirectory 

該文件存儲爲私有,並且不允許文件管理器讀取它

使用

file.externalDataDirectory 

存儲文件作爲公衆和允許其他應用程序打開它

請不要混淆externalDataDirecto ry as(SD卡)僅使用您的設備內部存儲器

相關問題