[英]FileTransfer Cordova download path
I'm using Cordova (5.4) to create apps for Android and Iphone. 我正在使用Cordova(5.4)创建适用于Android和Iphone的应用程序。 All goes fine, except I want to download images using the Cordova's plugin " FileTransfer " and I having some problems with the path.
一切正常,只不过我想使用Cordova的插件“ FileTransfer ”下载图像,并且路径出现问题。
If I use the FileTransfer like this: 如果我像这样使用FileTransfer:
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
}
}
);
This works fine. 这很好。 But I would want a path that worked on Android and Iphone, (not a static one) and if it could be, that the user could see this images directly in their gallery.
但是我想要一个在Android和Iphone上都可以使用的路径(不是静态路径),如果可以的话,用户可以直接在图库中看到这些图像。
Checking the plugin description I tried: 检查我尝试的插件说明:
fileURL = 'cdvfile://localhost/persistent/myImg.png'
But this fails with the FileTrasferError: 但这失败,出现FileTrasferError:
"/data/data/com.aco.plus/files/files/myImg.png: open failed: ENOTDIR (Not a directory)"
“ /data/data/com.aco.plus/files/files/myImg.png:打开失败:ENOTDIR(不是目录)”
Checking answers around I tried also: 检查我也尝试过的答案:
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
}
}
);
});
And I got the same error. 而且我遇到了同样的错误。
I'm quite lost. 我很迷路。 Anyone knows what can I do?
谁知道我该怎么办? I'm quite sure that must be a better way to do it than static routes.
我很确定这一定是比静态路由更好的方法。
@Luisma, @Luisma,
Please find the sample code snippet to write pdf file in device using cordova file and file transfer plugin: 请找到示例代码片段,以使用cordova文件和文件传输插件在设备中写入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
);
};
For paths into the application, I like to use 对于应用程序的路径,我喜欢使用
https://github.com/apache/cordova-plugin-file https://github.com/apache/cordova-plugin-file
This maps the different paths on every operative system, so its transparent to you, even through different SO or versions, it just pick the correct one. 这会映射每个操作系统上的不同路径,因此即使您使用不同的SO或版本,它也对您透明,它只是选择了正确的路径。
Happy coding! 编码愉快!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.