问题
I'm new on stackoverflow. it's the first time that I have to use Phonegap and really I have a problem. I need to make a table and by clicking on each element starts to download a pdf file and create a new folder (if it does not exist). But I can not even download a file after compiling with phonegap. All the examples I saw, just download an image through onload.
<script type="text/javascript">
function downloadFile(){
var url = 'http://http://legalespymes.com.ar/legalespymes/abonos/aseguradoras.pdf';
var filePath = 'local/path/to/your/file';
var fileTransfer = new FileTransfer();
var uri = encodeURI(url);
fileTransfer.download(
uri,
filePath,
function(entry) {
console.log("download complete: " + entry.fullPath);
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
},
false,
{
headers: {
"Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
}
}
);
}
</script>
HTML
<td onclick="downloadFile()">Row 1</td>
回答1:
First of all your url is invalid included http multiple times
var url = 'http://http://legalespymes.com.ar/legalespymes/abonos/aseguradoras.pdf';
change like this
var url = 'http://legalespymes.com.ar/legalespymes/abonos/aseguradoras.pdf';
If you are create multiple directory and store file inside that directory this may be create issue.(example [project/sample/local] not created same time. so file not download).And make sure file download plugins are available in config.xml and specify phonegap version.
Use this code to create multiple directory at time.
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
// create directory
function gotFS(fileSystem) {
window.FS = fileSystem;
var printDirPath = function(entry){
console.log("Dir path - " + entry.fullPath);
}
createDirectory("local/path/to/your", printDirPath);
}
function fail() {
console.log("failed to get filesystem");
}
function createDirectory(path, success){
var dirs = path.split("/").reverse();
var root = window.FS.root;
var createDir = function(dir){
console.log("create dir " + dir);
root.getDirectory(dir, {
create : true,
exclusive : false
}, successCB, failCB);
};
var successCB = function(entry){
root = entry;
if(dirs.length > 0){
createDir(dirs.pop());
}else{
success(entry);
}
};
var failCB = function(){
};
createDir(dirs.pop());
}
Now you write your file download code .
来源:https://stackoverflow.com/questions/24122799/phonegap-filetransfer-download