admin管理员组文章数量:1353150
I've ran into some problems while using cordova plugin File Transfer. Thats my code:
window.requestFileSystem(
LocalFileSystem.PERSISTENT,
0,
function onFileSystemSuccess(fileSystem) {
fileSystem.root.getFile(
"dummy.html", {create: true, exclusive: false},
function gotFileEntry(fileEntry) {
var sPath = fileEntry.fullPath.replace("dummy.html", "");
var fileTransfer = new FileTransfer();
fileEntry.remove();
fileTransfer.download( '.png', sPath + photo.original_name,
function (theFile) {
alert('success: ' + JSON.stringify(theFile));
console.log("download plete: " + theFile.toURI());
// showLink(theFile.toURI());
},
function (error) {
alert('error: ' + JSON.stringify(error));
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code: " + error.code);
},
true
);
})
},
function (error) {
alert('error request: ' + JSON.stringify(error));
}
);
The fileTransfer.download's error callback is being returned with error code 3, http 401. I already updated the File and FileTransfer plugins, my cordova version is 4.3.0. Also checked my config.xml for
<access origin="*" />
but it's there. I tried adding the header Connection: close, but no result. Tried setting the download's 4th param to its default (false) too - no luck.
Testing on Android tablet.
Anyone anything? Thanks!
I've ran into some problems while using cordova plugin File Transfer. Thats my code:
window.requestFileSystem(
LocalFileSystem.PERSISTENT,
0,
function onFileSystemSuccess(fileSystem) {
fileSystem.root.getFile(
"dummy.html", {create: true, exclusive: false},
function gotFileEntry(fileEntry) {
var sPath = fileEntry.fullPath.replace("dummy.html", "");
var fileTransfer = new FileTransfer();
fileEntry.remove();
fileTransfer.download( 'http://cordova.apache/images/cordova_bot.png', sPath + photo.original_name,
function (theFile) {
alert('success: ' + JSON.stringify(theFile));
console.log("download plete: " + theFile.toURI());
// showLink(theFile.toURI());
},
function (error) {
alert('error: ' + JSON.stringify(error));
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code: " + error.code);
},
true
);
})
},
function (error) {
alert('error request: ' + JSON.stringify(error));
}
);
The fileTransfer.download's error callback is being returned with error code 3, http 401. I already updated the File and FileTransfer plugins, my cordova version is 4.3.0. Also checked my config.xml for
<access origin="*" />
but it's there. I tried adding the header Connection: close, but no result. Tried setting the download's 4th param to its default (false) too - no luck.
Testing on Android tablet.
Anyone anything? Thanks!
Share Improve this question edited Jun 19, 2016 at 7:39 Paolo Stefan 10.3k5 gold badges46 silver badges65 bronze badges asked Apr 14, 2015 at 16:03 radzikradzik 3721 gold badge5 silver badges22 bronze badges 6- does the tablet have internet connection? error 3 y a connection problem – jcesarmobile Commented Apr 15, 2015 at 13:48
- Yeah, it does. The app returns an error with code 3 and http_status 401, body: null, exception: null – radzik Commented Apr 15, 2015 at 14:18
- the url is the one on your example? – jcesarmobile Commented Apr 15, 2015 at 14:23
- Yes, the same (cordova.apache/images/cordova_bot.png) is in my code right now - I put that one because I think this should work. But unfortunately it doesnt. – radzik Commented Apr 15, 2015 at 14:26
-
Which version of Cordova-android? Obtained by running
cordova platform ls
. – keldar Commented Apr 16, 2015 at 21:21
6 Answers
Reset to default 4Just found a "solution" to my problem. What I did was to downgrade the file-transfer plugin version from 0.5.0 to 0.4.8.
If someone ever face similar problem, do as below:
- Delete the existing file-transfer plugin by running 'cordova plugins list' and then 'cordova plugin remove name_from_list'
- Go to https://github./apache/cordova-plugin-file-transfer/releases and download the zip file of 0.4.8 release.
- Unzip the file in root of your cordova application
- Run 'cordova plugin add path_to_unzipped_folder'
That's it. Seems to be working well, at least the success callback is returned, didn't really test more of it.
I personally wouldn't create a file then remove it just to get a directory URL. You should be able to obtain just that by doing fileSystem.root.toURL()
- fileSystem.root is a DirectoryEntry and so contains methods you'd expect to see on a DirectoryEntry.
Just a bit quicker.
Update
If you feel inclined to use the file delete method, you should be using toURL() on the FileEntry, not fullPath. I think toURL() returns a URL that can be used throughout HTML app.
But as I say, fileSystem.root.toURL() is preferable. Example code as follows:
Thus, your code bees:
window.requestFileSystem(
LocalFileSystem.PERSISTENT,
0,
function (fileSystem) {
var url = 'http://cordova.apache/images/cordova_bot.png',
dir = fileSystem.root.toURL() + photo.original_name,
ft = new FileTransfer();
ft.download(url, dir,
function (fileEntry) {
alert('Downloaded!');
},
function (error) {
alert('Download error');
console.log(dir);
}
);
},
function (error) {
alert('Error getting file system');
}
);
Try that and see what happens. Might sound stupid, but I presume photo.original_name
is defined? And there's a '/' between the directory and the filename?
If your remote file needs VPN to access, make sure your device is connected to VPN.
In my case I was not connected to VPN on device.
Just change the download url to http://www.{{Domain name}}/abc.pdf
use full url with www
This worked for me
let pathToDownload = `${cordova.file.externalDataDirectory}`;
let fileTransfer = new FileTransfer();
let url = encodeURI(`https://cordova.apache/images/cordova_bot.png`);
fileTransfer.download(url, pathToDownload + "image.png",
(fileEntry) => {
console.log('Downloaded!');
},
(error) => {
console.log('Download error');
},
);
My problem was related to ad http (not https) remote file url.
I solved with this steps:
- Opening my Project target's info.plist file
- Adding a Key called NSAppTransportSecurity as a Dictionary.
- Added a Subkey called NSAllowsArbitraryLoads as Boolean and set its value to YES.
RefLink: The resource could not be loaded because the App Transport Security policy requires the use of a secure connection
本文标签: javascriptCordova FileTransfer Downloadalways returns error 3Stack Overflow
版权声明:本文标题:javascript - Cordova FileTransfer Download - always returns error 3 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743921300a2562141.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论