相关:https://stackoverflow.com/questions/21044197/download-file-and-store-them-locally-in-sdcard-using-phonegapbuild但这些问题从未得到解答。
关于这个应用程序,我正在编写一个显示一堆 pdf 的小应用程序,它是使用 jquerymobile + PhoneGap 编写的,并且是使用 PhoneGap 构建服务构建的。
我意识到这个函数无法工作,因为根目录不是写入任何内容的有效位置。但是,我无法获取应用程序的工作目录,也无法使用文档弄清楚错误代码 1 的确切含义。
来自 config.xml:
<preference name="AndroidPersistentFileLocation" value="Internal" />
<preference name="permissions" value="none" />
<feature name="http://api.phonegap.com/1.0/file"/>
<feature name="http://api.phonegap.com/1.0/device" />
<gap:plugin name="org.apache.cordova.file" />
<gap:plugin name="org.apache.cordova.file-transfer" />
构建服务告诉我这些插件已成功添加。
解压 apk 并查看 list 还可以发现
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
已成功添加。
最后但并非最不重要的一点是,phonegap 库已成功添加到文档中
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
然后就只剩下实际的函数了:
function downloadFile(name, url, y,x, canvasid, wrapperid){
alert(name);
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function(fileSystem) {
var directoryEntry = fileSystem.root; // to get root path to directory
var rootdir = fileSystem.root;
var fp = rootdir.fullPath;
fp = fp+"/bestsongs/"+name;
alert("downloading: " + url + " to " + fp);
var fileTransfer = new FileTransfer();
alert("downloading: " + url + " to " + fp);
fileTransfer.download(
url,
name,
function(theFile) {
alert("download complete: " + theFile.toURI());
loadPDF(theFile.toURI(), y,x, canvasid, wrapperid);
},function(error) {
alert("error: " + JSON.stringify(error));
}
);
},
function(arg) {
alert("FAIL ON requestFileSystem" + JSON.stringify(arg)); }
);
}
$(function() {
document.addEventListener("deviceready", function() {
alert("deviceready!");
try {
alert(url);
downloadFile(lyrics,url,y,x, 'the-canvas', 'wrapper');
} catch(e) {
alert(e);
}
}, false);
});
使用警报,我得到的结果如下:
deviceready!
<url> to file (which is right)
the name of the file.
downloading <url> to //bestsongs/<name>
downloading <url> to //bestsongs/<name>
error: {
"code":1,
"source":<url>,
"target":<name>,
"http_status": null,
"body": null
}
我已经调试了好几天了,请有人提供解决方案。
最佳答案
嘿,看看我在这里的回答 -
Phonegap - Save image from url into device photo gallery
并让我知道。如果还有问题的话。 在您的 config.xml 中包含以下内容
对于 Android -
<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>
对于 IOS-
<plugin name="File" value="CDVFile" />
<plugin name="FileTransfer" value="CDVFileTransfer" />
要检查网络连接是否可用,请在 config.xml 中添加以下内容 - 在plugin.xml中添加权限,在config.xml中添加插件名称
检查网络连接的插件 对于 Android -
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager" />
对于 IOS -
<plugin name="NetworkStatus" value="CDVConnection" />
关于javascript - PhoneGap文件传输错误1、哪里写FileTransfers?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22294547/