2016-02-19 71 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