Phonegap Filetransfer下载

我是新的在stackoverflow。 这是我第一次使用Phonegap,真的有一个问题。 我需要制作一个表格,并通过点击每个元素开始下载一个PDF文件,并创build一个新的文件夹(如果它不存在)。 但是用phonegap编译后甚至无法下载文件。 我看到的所有例子,只是通过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> 

首先你的url无效,包括http多次

 var url = 'http://http://legalespymes.com.ar/legalespymes/abonos/aseguradoras.pdf'; 

像这样改变

 var url = 'http://legalespymes.com.ar/legalespymes/abonos/aseguradoras.pdf'; 

如果你在这个目录下创build多个目录和存储文件,这可能是创build问题(例如[project / sample / local]不是同一时间创build的,所以文件不能下载)。确保文件下载插件在config.xml并指定手机版本。

使用此代码在时间创build多个目录。

 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()); } 

现在你写你的文件下载代码。