phonegap download() 下载路径问题

本文详细介绍了如何在更新到新版3.5后,利用FileEntry.toURL()和DirectoryEntry.toURL()方法进行文件系统操作,包括获取应用目录、下载文件并显示文件路径等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以前的版本用entry.fullPath,是可行,

但更新新版3.5之后就不行要用FileEntry.toURL() and DirectoryEntry.toURL()


贴上别人的例子

window.appRootDirName = "download_photo"; 
    document.addEventListener("deviceready", onDeviceReady, false); 




    function onDeviceReady() { 
        console.log("device is ready"); 
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
    } 




    function fail() { 
        console.log("failed to get filesystem"); 
    } 




    function gotFS(fileSystem) { 
        console.log("filesystem got"); 
        window.fileSystem = fileSystem; 
        fileSystem.root.getDirectory(window.appRootDirName, { 
            create : true, 
            exclusive : false 
        }, dirReady, fail); 
    } 




    function dirReady(entry) { 
        window.appRootDir = entry; 
        console.log("application dir is ready"); 
    } 








    function downloadFile(fileurl){ 
         var uri = encodeURI(fileurl);//定义下载的路径

//var filePath = window.appRootDir.fullPath+uri.substr(uri.lastIndexOf('/') + 1);主要这里以前是这样子
         var filePath =
window.appRootDir.toURL()+uri.substr(uri.lastIndexOf('/') + 1);
       
        alert("start"); 
        var fileTransfer = new FileTransfer(); 
         
        alert(window.appRootDir.fullPath); 
        fileTransfer.download( 
            uri, 
            filePath, 
            function(entry) { 
                alert("success"); 
                alert(entry.fullPath); 
                //此处调用打开文件方法 
                //OpenFile(entry.fullPath); 
                //window.location.href = window.appRootDir.fullPath; 
                console.log("download complete: " + entry.fullPath); 
            }, 
            function(error) { 
                alert("error"); 
                alert(JSON.stringify(error)); 
                console.log("download error source " + error.source); 
                console.log("download error target " + error.target); 
                console.log("upload error code" + error.code); 
            } 
        )} 
        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值