我一直在使用 phonegap 并试图将壁纸 jpg 下载到设备相机胶卷。我一直在连接到 phonegap 服务器的手机 phonegap 应用程序上对此进行测试。我使用的代码如下:
function DownloadFile(URL, Folder_Name, File_Name) {
if (URL == null && Folder_Name == null && File_Name == null) {
return;
}
else {
download(URL, Folder_Name, File_Name); //If available download function
}
}
function download(URL, Folder_Name, File_Name) {
//step to request a file system
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fileSystemSuccess, fileSystemFail);
function fileSystemSuccess(fileSystem) {
var download_link = encodeURI(URL);
ext = download_link.substr(download_link.lastIndexOf('.') + 1); //Get extension of URL
var directoryEntry = fileSystem.root; // to get root path of directory
directoryEntry.getDirectory(Folder_Name, { create: true, exclusive: false }, onDirectorySuccess, onDirectoryFail); // creating folder in sdcard
var rootdir = fileSystem.root;
var fp = rootdir.toURL(); // Returns Fulpath of local directory
fp = fp + Folder_Name +"/"+ File_Name + ".jpg"; // fullpath and name of the file which we want to give
filetransfer(download_link, fp);
}
function onDirectorySuccess(parent) {
// Directory created successfuly
}
function onDirectoryFail(error) {
//Error while creating directory
alert("Unable to create new directory: " + error.code);
}
function fileSystemFail(evt) {
//Unable to access file system
alert(evt.target.error.code);
}
}
function filetransfer(download_link, fp) {
var fileTransfer = new FileTransfer();
// File download function with URL and local path
fileTransfer.download(download_link, fp,
function (entry) {
console.log("cdvfile://localhost/persistent/" + fp);
alert("download complete: " + entry.fullPath.slice(1));
},
function (error) {
//Download abort errors or download failed errors
alert("download error source " + error.source);
alert("download error target " + error.target);
//alert("upload error code" + error.code);
}
);
}
有趣的是,当我单击触发这些方法的按钮时,我会收到一条警报,提示下载完成:file_name。但是,我的相机胶卷中从未出现图像。这是iOS问题吗?我已经尝试在本机上查看它,而不是用 phonegap serve 模拟它,它没有任何区别。
我也尝试过这样的 file_path "cdvfile://localhost/persistent/"+Folder_name+"/"+ File_name+"."+ext;
,但没有不同的结果。
在不同的帖子http://community.phonegap.com/nitobi/topics/file_download_not_working_properly_on_ios中,iOS 的答案是使用
<access origin="*" />
我确保在我的 config.xml 文件中使用它。
有人知道我错过了什么吗?该代码对其他人有效,奇怪的是它表明它正确完成。