phonegap的下载方法及文件系统

参考:http://docs.phonegap.com/en/2.9.0/cordova_file_file.md.html#File


phonegap的下载方法:

var fileTransfer = new FileTransfer();
var uri = encodeURI("http://www.google.com.hk/images/srpr/logo4w.png");

fileTransfer.download(
    uri,
    filePath,/*filePath的根目录:/var/mobile/Applications/4CBF4219-B2D2-420C-B0CC-32CA1D6DD42E/Documents/ 程序内www的路径为:var/mobile/Applications/4CBF4219-B2D2-420C-B0CC-32CA1D6DD42E/middleware.app/www/   当phonegap加载完成后会console.log出中间的唯一标识,也可以通过jquery获取加载过来的某个js文件的路径来获得,应该注意的是,这里的filePath==/var/mobile/Applications/4CBF4219-B2D2-420C-B0CC-32CA1D6DD42E/Documents/logo4w.png*/
    function(entry) {   /*成功下载回调函数*/
        console.log("download complete: " + entry.fullPath);/*下载的文件的完整路径*/
    },
    /*下载失败回调函数*/
    function(error) {
        console.log("download error source " + error.source);
        console.log("download error target " + error.target);
        console.log("upload error code" + error.code);
    },
    false,/*是否接受所有的安全证书。官方解释为:Optional parameter, defaults to false. If set to true then it will accept all security certificates. This is useful as Android rejects self signed security certificates. Not recommended for production use. Supported on Android and iOS. (boolean)*/
    {
        headers: {
            "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
        }
    }/*官方解释为:Optional parameters, currently only supports headers (such as Authorization (Basic Authentication), etc).*/
);
//注:一般我都不用最后两个参数...


Phonegap的文件系统:

phonegap中要对文件操作,则应该先请求一个文件系统,成功后获得fileSystem对象后才可用,示例如下:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);

function onFileSystemSuccess(fileSystem) {
    console.log("fileSystem.name:-------"+fileSystem.name);
    console.log("fileSystem.root.name:-------"+fileSystem.root.name);
    
    //创建目录
    fileSystem.root.getDirectory("file_mobile/download", {create:true},
                                 function(fileEntry){ console.log("创建目录成功,完整路径为:"+fileEntry.fullPath); },
                                 function(){  console.log("创建目录失败");});
/*getDurectiry的官方解释:path: The path to the directory to be looked up or created. Either an absolute path, or a relative path from this DirectoryEntry. (DOMString)
options: Options to specify whether the directory is to be created if it doesn't exist. (Flags)
successCallback: A callback that executes with a DirectoryEntry object. (Function)
errorCallback: A callback that executes if an error occurs when creating or looking up the directory. Invoked with a FileError object. (Function)*/
    
    var _localFile = "file_mobile/download/logo4w.png";
    var _url = "http://www.google.com.hk/images/srpr/logo4w.png";
    //查找文件
    fileSystem.root.getFile(_localFile, {create:true}, function(fileEntry){
                            console.log("fileEntry.fullPath="+fileEntry.fullPath);                                            
                            }, function(){              
                            });
/*getFile的官方解释:path: The path to the file to be looked up or created. Either an absolute path, or a relative path from this DirectoryEntry. (DOMString)
options: Options to specify whether the file is created if it doesn't exist. (Flags)
successCallback: A callback that is passed a FileEntry object. (Function)
errorCallback: A callback that executes if an error occurs when creating or looking up the file. Invoked with a FileError object. (Function)*/
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值