使用PhoneGap在Windows手机上下载文件

时间:2016-01-15 14:29:43

标签: windows file cordova windows-phone-8.1 phonegap-plugins

借助此链接“https://gist.github.com/nathanpc/2464060”,我编写了代码来下载PhoneGap中的文件。我在Android手机上试过它,效果很好。 但是在Windows手机上,它说它已经下载了,路径是 - //xyz/SIFA.pdf。

要查看Windows手机上的文件系统,我已经下载了一个名为“文件”的应用程序,我在手机上查找但我找不到任何文件夹xyz或文件。

我还将Windows手机连接到我的电脑上看了一下但找不到文件夹也没找到pdf

你能否在代码中建议我做错了什么,或者我是不是以正确的方式寻找文件。

感谢您的提前帮助

这是我在PhoneGap中下载文件的代码。 我已添加警报以指示进度。

var folderName = 'xyz';
var fileName;

var downloadFileOne = function (URL) {
//step to request a file system 
alert("step one");
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fileSystemSuccess, fileSystemFail);
alert("step two");
function fileSystemSuccess(fileSystem) {
    alert("step three");
    var download_link = encodeURI(URL);
    alert("step four - " + download_link);
    fileName = download_link.substr(download_link.lastIndexOf('/') + 1); //Get filename of URL
    var directoryEntry = fileSystem.root; // to get root path of directory
    directoryEntry.getDirectory(folderName, {
        create: true,
        exclusive: false
    }, onDirectorySuccess, onDirectoryFail); // creating folder in sdcard
    alert("step five");
    var rootdir = fileSystem.root;
    var fp = fileSystem.root.toNativeURL(); // Returns Fullpath of local directory

    fp = fp + "/" + folderName + "/" + fileName; // fullpath and name of the file which we want to give
    // download function call
    filetransfer(download_link, fp);
}

function onDirectorySuccess(parent) {
    // Directory created successfuly
    alert("directory created successfully");
}

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) {
alert("step six - file transfer method");
var fileTransfer = new FileTransfer();
// File download function with URL and local path
fileTransfer.download(download_link, fp,
    function (entry) {
        alert("download complete: " + entry.fullPath);
    },
    function (error) {
        //Download abort errors or download failed errors
        alert("download error source " + error.source);
    }
);
}

0 个答案:

没有答案