问题
I have a simple hybrid app which should download files with cordova 2.3.0. From the documentation:
var filePath = someFullPath; //e.g. "file:///mnt/sdcard/myfile.pdf"
var uri = encodeURI("http://someurl.com/myfile.pdf");
var fileTransfer = new FileTransfer();
fileTransfer.download(
uri,
filePath,
function(entry) {
console.error("download complete: " + entry.fullPath);
},
function(error) {
console.error("download error source " + error.source);
console.error("download error target " + error.target);
console.error("donwload error code " + error.code);
console.error("http: "+error.http_status);
}
);
}
The outcome is error 3, Connection_Err, with http_status 401, the resource myfile.pdf is public on a http server with no auth required.
I have tried both creating the file file://mnt/sdcard/myfile.pdf with getFile("my file.pdf", {create: true, exclusive: false}, success, fail); or just setting a string to an existing directory. It didn't work in both scenarios.
Can you please advise? I have to use cordova 2.3.0.
UPDATE:
I think my problem is very close to this one: FileTransfer in Phonegap code 401
I have Android API 17, cordova 2.3.0, I need to set allow origin = "*", but I can't create the config.xml file, for some reasons it throws errors. Have you faced this problem?
I created the folder xml inside res (there wasn't one) and created a file config.xml inside such folder. The content of the config.xml is
<access origin="*"></access>
when I run the application I get an alert with this error: Error Initializing Cordova: Class not found
回答1:
All the code was correct, it was a problem with my configuration of the framework:
this project had another project as library dependence. This project didn't have a config.xml file where to whitelist URLs. The library project had! I just needed to add the
<access origin="http://*.somedomain.com"></access>
to the "right" config.xml cordova file, which was in a Library project, not in the main project itself.
回答2:
In my setup, having access was not sufficient. I added plugin cordova-plugin-whitelist to my app. Then it worked fine.
来源:https://stackoverflow.com/questions/24124217/cordova-filetransfer-download-error