Kei*_*ith 3 android file-transfer android-file phonegap-plugins cordova
我正在将文件下载到本地文件系统.我可以通过fileSystem.root.getFile成功创建空文件,但fileTransfer.download使用FILE_NOT_FOUND_ERR失败,即使它使用相同的路径.
问题是我的文件是在//sdcard/MyDir/test.pdf(我使用adb shell确认)创建的,但fileEntry返回了一个没有 sdcard 的路径://MyDir/test.pdf.fileTransfer.download因此路径失败.它也失败了相对路径MyDir/test.pdf.
如果我用'sdcard'硬编码完整路径,我可以避免使用FILE_NOT_FOUND_ERR(具体来说,在FileTransfer.java中resourceApi.mapUriToFile调用成功),但后来我得到一个CONNECTION_ERR并且控制台显示"文件插件不能代表下载路径".(在FileTransfer.java中,filePlugin.getEntryForFile调用返回null.我假设它不喜欢路径中的'sdcard'.)
有没有更好的方法在fileTransfer.download中指定目标路径?
var downloadUrl = "http://mysite/test.pdf";
var relativeFilePath = "MyDir/test.pdf"; // using an absolute path also does not work
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
fileSystem.root.getFile(relativeFilePath, { create: true }, function (fileEntry) {
console.log(fileEntry.fullPath); // outputs: "//MyDir/test.pdf"
var fileTransfer = new FileTransfer();
fileTransfer.download(
downloadUrl,
/********************************************************/
/* THE PROBLEM IS HERE */
/* These paths fail with FILE_NOT_FOUND_ERR */
//fileEntry.fullPath, // this path fails. it's "//MyDir/test.pdf"
//relativeFilePath, // this path fails. it's "MyDir/test.pdf"
/* This path gets past the FILE_NOT_FOUND_ERR but generates a CONNECTION_ERR */
"//sdcard/MyDir/test.pdf"
/********************************************************/
function (entry) {
console.log("Success");
},
function (error) {
console.log("Error during download. Code = " + error.code);
}
);
});
});
Run Code Online (Sandbox Code Playgroud)
我正在使用Android SDK模拟器,如果这有所作为.
Kei*_*ith 12
我能够通过使用文件路径的URI来解决这个问题.(我还根据@Regent的评论删除了对fileSystem.root.getFile的不必要调用.)
var downloadUrl = "http://mysite/test.pdf";
var relativeFilePath = "MyDir/test.pdf"; // using an absolute path also does not work
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
var fileTransfer = new FileTransfer();
fileTransfer.download(
downloadUrl,
// The correct path!
fileSystem.root.toURL() + '/' + relativeFilePath,
function (entry) {
console.log("Success");
},
function (error) {
console.log("Error during download. Code = " + error.code);
}
);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6480 次 |
| 最近记录: |