2016-02-19 69 views
2

我正在使用Cordova(5.4)爲Android和Iphone創建應用程序。一切都很好,除非我想用科爾多瓦的插件「FileTransfer」下載圖片,並且我在路徑上遇到了一些問題。FileTransfer Cordova下載路徑

如果我使用文件傳輸這樣的:

 uri = encodeURI('http://example.com/myImage.png'), 
      fileURL = '/sdcard/Download/' + 'myImage.png', 
fileTransfer.download(
       uri, 
       fileURL, 
       function (entry) { 
        console.log("download complete: " + entry.fullPath); 
       }, 
       function (error) { 
        console.log(error); 
       }, 
       false, 
       { 
        headers: { 
         "authorization": 'Bearer ' + token 
        } 
       } 
      ); 

這工作得很好。但是我想要一個適用於Android和Iphone(不是靜態的)的路徑,並且如果可能的話,用戶可以直接在他們的畫廊中看到這些圖像。

檢查我試過插件描述:

fileURL = 'cdvfile://localhost/persistent/myImg.png' 

但這種失敗與FileTrasferError:

"/data/data/com.aco.plus/files/files/myImg.png: open failed: ENOTDIR (Not a directory)"

檢查答案圍繞我想也是:

uri = encodeURI('http://example.com/myImage.png'); 

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) { 

      fileTransfer.download(
       uri, 
       fileSystem.root.toURL() + '/' + 'myImg.png', 
       function (entry) { 
        console.log("download complete: " + entry.fullPath); 
       }, 
       function (error) { 
        console.log(error); 

       }, 
       false, 
       { 
        headers: { 
         "authorization": 'Bearer ' + token 
        } 
       } 
      ); 
     }); 

而且我得到了同樣的錯誤。

我很迷茫。任何人都知道我能做什麼?我相當肯定,這是一個比靜態路由更好的方式。

+0

我知道它很老的問題,但你能解決這個問題問題。因爲我在將文件保存到下載文件夾時也遇到了麻煩。 – Ironic

+0

如果下載成功,您應該重新掃描設備存儲,因爲Cordova不知道文件是否已下載。所以我做了一個插件,它是一個插件,下載後更新畫廊。 [cordova-plugin-gallery-refresh](https://www.npmjs.com/package/cordova-plugin-gallery-refresh) –

回答

2

@Luisma,

請找到示例代碼段使用科爾多瓦文件和文件傳輸的插件寫在設備的PDF文件:

var fileTransfer = new FileTransfer(); 

if (sessionStorage.platform.toLowerCase() == "android") { 
    window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, onFileSystemSuccess, onError); 
} else { 
    // for iOS 
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onError); 
} 

function onError(e) { 
    navigator.notification.alert("Error : Downloading Failed"); 
}; 

function onFileSystemSuccess(fileSystem) { 
    var entry = ""; 
    if (sessionStorage.platform.toLowerCase() == "android") { 
     entry = fileSystem; 
    } else { 
     entry = fileSystem.root; 
    } 
    entry.getDirectory("Cordova", { 
     create: true, 
     exclusive: false 
    }, onGetDirectorySuccess, onGetDirectoryFail); 
}; 

function onGetDirectorySuccess(dir) { 
    cdr = dir; 
    dir.getFile(filename, { 
     create: true, 
     exclusive: false 
    }, gotFileEntry, errorHandler); 
}; 

function gotFileEntry(fileEntry) { 
    // URL in which the pdf is available 
    var documentUrl = "http://localhost:8080/testapp/test.pdf"; 
    var uri = encodeURI(documentUrl); 
    fileTransfer.download(uri, cdr.nativeURL + "test.pdf", 
     function(entry) { 
      // Logic to open file using file opener plugin 
     }, 
     function(error) { 
      navigator.notification.alert(ajaxErrorMsg); 
     }, 
     false 
    ); 
}; 
+0

嗨甘地,如果你應該找到重複,請標記他們這樣的。 – bummi

+0

@bummi,能否請你指導我,因爲我對此很陌生?在此先感謝 – Gandhi

+0

此問題和您[也回答](http:// stackoverflow。com/q/36130527/1699210)共享相同的答案,這可能表明應該關閉爲另一個副本。我對科爾多瓦不熟悉,所以我沒有舉報/投票。如果你認爲重複只是將其中一個標記爲更好的副本。選擇標誌/重複和目標的鏈接。 – bummi

1

對於路徑到應用程序中,我喜歡用

https://github.com/apache/cordova-plugin-file

此映射每個手術系統上的不同路徑,所以其透明的你,甚至可以通過SO或版本不同,它只是挑正確的一個。

編碼愉快!

+0

非常感謝您的幫助,並對評論中的延遲表示歉意。我已經在使用你說的插件了,但我認爲我做錯了什麼或者我錯過了什麼: 例如,當我使用cordova.file.applicationStorageDirectory +'/'+ fName時,它說它存儲「/data/data/com.aco.plus/myImg.jpg」中的文件,但沒有/ data/data目錄,並且該映像不在文件系統中(以及爲什麼/ data/data似乎重複)。 如果我使用類似「cdvfile:// localhost/persistent/Download /」+ fName的東西,我得到了一個像我之前展示的那樣的FileTrasferError。 我在做什麼錯了? – Luisma