Javascript 在IOS上使用url下载Phonegap图像

Javascript 在IOS上使用url下载Phonegap图像,javascript,ios,cordova,file-transfer,imagedownload,Javascript,Ios,Cordova,File Transfer,Imagedownload,我一直在使用phonegap,正在尝试将墙纸jpgs下载到设备的摄像头上。我一直在连接到phonegap服务器的移动phonegap应用程序上测试这一点。我使用的代码如下所示: function DownloadFile(URL, Folder_Name, File_Name) { if (URL == null && Folder_Name == null && File_Name == null) { return; } else

我一直在使用phonegap,正在尝试将墙纸jpgs下载到设备的摄像头上。我一直在连接到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);
             }
        );
   }
有趣的是,当我点击触发这些方法的按钮时,我会收到一个警报,上面写着下载完成:文件名。但是,在我的相机卷中从未出现过图像。这是iOS问题吗?我试着从本机的角度来看待这一点,而不是用phonegap服务来模拟它,这没有什么区别

我也尝试过这样的文件路径cdvfile://localhost/persistent/“+文件夹名称+”/“+文件名称+”+ext 没有不同的结果

在另一篇帖子中,iOS的答案是使用

<access origin="*" />

我确保在config.xml文件中使用它


有人知道我遗漏了什么吗?此代码适用于其他人,奇怪的是,它正确地说明了它的完成情况。

有人想出了解决方案吗?我的代码看起来是正确的,其他人已经让它工作了,但有些事情是不正确的。我相信你的代码可以将图像(或任何文件)存储到应用程序的文件系统中。我不相信cordova.fileTransfer能够将照片写入您的相机卷。为此,请尝试以下插件: