Phonegap 文件下载问题
Phonegap File Download Issue
我正在尝试将远程文件下载到我的设备。
我正在使用 PhoneGap Build。这是我的代码。
我尝试了我找到的每一段代码。当我点击下载按钮时,没有任何反应。在调试时,没有错误显示。
我哪里弄错了?
<script type="text/javascript">
window.appRootDirName = "my_app";
function onLoad(){
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
console.log("device is ready");
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
console.log("filesystem got");
window.fileSystem = fileSystem;
fileSystem.root.getDirectory(window.appRootDirName, {
create : true,
exclusive : false
}, dirReady, fail);
}
function dirReady(entry) {
window.appRootDir = entry;
console.log('application dir is ready');
}
function fail() {
console.log('failed to get filesystem');
}
function downloadFile(url,fileName) {
var fileTransfer = new FileTransfer();
var filePath = window.appRootDir.fullPath + "/" + fileName + ".txt";
fileTransfer.download(url, filePath, function(entry) {
alert("download complete: " + entry.fullPath);
}, function(error) {
console.log(error);
});
}
$(document).ready(function(){
.... some other code
//on click download file
$(document).on('click touchstart', '.download', function(e){
e && e.preventDefault();
var url = $(this).data('url');
var title = $(this).data('title');
downloadFile(url,title);
});
});
</script>
还有config.xml
<gap:plugin name="com.coconutcenter.file-transfer" version="0.4.6" />
<feature name="http://api.phonegap.com/1.0/file" />
<feature name="File">
<param name="android-package" value="org.apache.cordova.file.FileUtils" />
</feature>
<feature name="FileTransfer">
<param name="android-package" value="org.apache.cordova.filetransfer.FileTransfer" />
</feature>
<feature name="Storage">
<param value="org.apache.cordova.Storage" name="android-package"/>
</feature>
.download 方法的错误代码:
{"code":1,"source":"http://myurl.com/file.txt","target":"//my_app/file.txt","http_status":null,"body":null}
终于我找到了解决办法。正如 提到的,我使用了 .toUrl()
insetad fullPath
。所以我的 downloadFile 函数是;
function downloadFile(url,fileName) {
var fileTransfer = new FileTransfer();
var filePath = window.appRootDir.toUrl() + "/" + fileName + ".txt"; //here
fileTransfer.download(url, filePath, function(entry) {
alert("download complete: " + entry.fullPath);
}, function(error) {
console.log(error);
});
}
我终于找到了解决办法。如前所述 ,我使用 .toUrl()
而不是 fullPath
我正在尝试将远程文件下载到我的设备。 我正在使用 PhoneGap Build。这是我的代码。
我尝试了我找到的每一段代码。当我点击下载按钮时,没有任何反应。在调试时,没有错误显示。
我哪里弄错了?
<script type="text/javascript">
window.appRootDirName = "my_app";
function onLoad(){
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
console.log("device is ready");
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
console.log("filesystem got");
window.fileSystem = fileSystem;
fileSystem.root.getDirectory(window.appRootDirName, {
create : true,
exclusive : false
}, dirReady, fail);
}
function dirReady(entry) {
window.appRootDir = entry;
console.log('application dir is ready');
}
function fail() {
console.log('failed to get filesystem');
}
function downloadFile(url,fileName) {
var fileTransfer = new FileTransfer();
var filePath = window.appRootDir.fullPath + "/" + fileName + ".txt";
fileTransfer.download(url, filePath, function(entry) {
alert("download complete: " + entry.fullPath);
}, function(error) {
console.log(error);
});
}
$(document).ready(function(){
.... some other code
//on click download file
$(document).on('click touchstart', '.download', function(e){
e && e.preventDefault();
var url = $(this).data('url');
var title = $(this).data('title');
downloadFile(url,title);
});
});
</script>
还有config.xml
<gap:plugin name="com.coconutcenter.file-transfer" version="0.4.6" />
<feature name="http://api.phonegap.com/1.0/file" />
<feature name="File">
<param name="android-package" value="org.apache.cordova.file.FileUtils" />
</feature>
<feature name="FileTransfer">
<param name="android-package" value="org.apache.cordova.filetransfer.FileTransfer" />
</feature>
<feature name="Storage">
<param value="org.apache.cordova.Storage" name="android-package"/>
</feature>
.download 方法的错误代码:
{"code":1,"source":"http://myurl.com/file.txt","target":"//my_app/file.txt","http_status":null,"body":null}
终于我找到了解决办法。正如 .toUrl()
insetad fullPath
。所以我的 downloadFile 函数是;
function downloadFile(url,fileName) {
var fileTransfer = new FileTransfer();
var filePath = window.appRootDir.toUrl() + "/" + fileName + ".txt"; //here
fileTransfer.download(url, filePath, function(entry) {
alert("download complete: " + entry.fullPath);
}, function(error) {
console.log(error);
});
}
我终于找到了解决办法。如前所述 .toUrl()
而不是 fullPath