如何使用Ionic Framework将文件下载到Download的目录?

时间:2017-01-05 17:22:45

标签: javascript android ionic-framework download

我正在尝试使用ngCordova,但cordova.file.documentsDirectory属性为null

我还尝试将ngCordovarequestFileSystem结合使用,但是,该文件仍未保存在“下载”中。

我的代码示例:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(directory) {
    var fileTransfer = new FileTransfer();
    fileTransfer.download(
        encodeURI("http://example.com/sample.pdf"),
        directory.root.nativeURL + 'sample.pdf',
        function(entry) {
            console.log("download complete: " + entry.fullPath);
        },
        function(error) {
            console.log("download error source " + error.source);
            console.log("download error target " + error.target);
            console.log("upload error code" + error.code);
        },
        false
    );
}, function(err){console.error(err)});

感谢。

2 个答案:

答案 0 :(得分:6)

def findInTupleA(elem,tuplo): lista_indices = [] i = 0 while i < len(tuplo): if tuplo[i] == elem: lista_indices.append(i) i = i + 1 if len(lista_indices) >= 1: return lista_indices else: raise IndexError("element not in tuple") 代替cordova.file.externalRootDirectory + '/Download/' + 'sample.pdf'

答案 1 :(得分:0)

我通过以下代码解决了该问题:

const ROOT_DIRECTORY = 'file:///sdcard//';
const downloadFolderName = 'Download';


this.file.createDir(ROOT_DIRECTORY, downloadFolderName, true)
  .then((entries) => {
 //then your code
 fileTransfer.download(fileLocation,this.ROOT_DIRECTORY+this.downloadFolderName+'/'+ 'sample.pdf').then((entry) => {
}
//ends of your code ^^
})
  .catch((error) => {
    alert('error' + JSON.stringify(error));
  });