【问题标题】:How to create a directory and file in that directory using phonegap file api?如何使用phonegap file api在该目录中创建目录和文件?
【发布时间】:2012-06-20 02:17:33
【问题描述】:

我正在使用phonegap file api创建一个目录,并在创建的目录中创建一个文件。正在创建目录,但未在目录中创建文件。

我使用的代码是:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function gotFS(fileSystem) {
    var dataDir = fileSystem.root.getDirectory("data", {create: true});
    var file = dataDir.getFile("lockfile.txt", {create: true, exclusive: true});
}

目录数据已创建,但lockfile.txt 未创建。

【问题讨论】:

    标签: javascript cordova ipad phonegap-build


    【解决方案1】:
    function download(URL, fileName){
       var folderName = 'xyz';
       var uri = encodeURI(URL);
    
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
                function (fileSystem) {
                    var directoryEntry = fileSystem.root; // to get root path of directory
                    directoryEntry.getDirectory(folderName, {
                        create: true,
                        exclusive: false
                    }, onDirectorySuccess, onDirectoryFail); 
                    var filename = fileSystem.root.toURL() + folderName + "/" + uri.substr(uri.lastIndexOf("/") + 1);
    
                    var fileTransfer = new FileTransfer();
                    fileTransfer.download(uri, filename,
                        function(entry) { // download success
                            var path = entry.toURL(); //**THIS IS WHAT I NEED**
                            window.plugins.toast.showLongBottom("Download Completed: " + entry.fullPath, function (a) {
                            }, function (b) {
                            });
                        },
                        function(error) {
                            console.log("error")
                        } // irrelevant download error
                    );`enter code here`
                },
                function(error) {
                    console.log("error2")
                } // irrelevant request fileSystem error
            );
    
            function onDirectorySuccess(parent) {
                // Directory created successfuly
                console.log("Directory created successfuly: " + JSON.stringify(parent));
                var fp = (parent.nativeURL) + fileName;
                filetransfer(download_link, fp);
            }
    
            function onDirectoryFail(error) {
                //Error while creating directory
                alert("Unable to create new directory: " + error.code);
            }
        }
    

    【讨论】:

    • 此下载代码适用于 ios 和 android 都具有最新版本的 cordova 3.4+ 全部。以及离子框架
    【解决方案2】:

    使用 phonegap 将文件从 url 下载到您的设备

    它适用于 iOS 和 android 3.0 及更高版本

    var folderName = 'xyz';
    var fileName;
    
    function downloadFile(URL) {
        //step to request a file system 
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fileSystemSuccess, fileSystemFail);
    
        function fileSystemSuccess(fileSystem) {
            var download_link = encodeURI(URL);
            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
            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
        }
    
        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) {
                alert("download complete: " + entry.fullPath);
            },
            function(error) {
                //Download abort errors or download failed errors
                alert("download error source " + error.source);
            }
        );
    }
    

    【讨论】:

      【解决方案3】:

      您需要以异步方式调用代码:

      document.addEventListener("deviceready", onDeviceReady, false);
      
      function onDeviceReady() {
          window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
      }
      
      function gotFS(fileSystem) {
         fileSystem.root.getDirectory("data", {create: true}, gotDir);
      }
      
      function gotDir(dirEntry) {
          dirEntry.getFile("lockfile.txt", {create: true, exclusive: true}, gotFile);
      }
      
      function gotFile(fileEntry) {
          // Do something with fileEntry here
      }
      

      【讨论】:

      • 那么,否决这个答案的人是否想解释为什么它对他们不起作用?
      • 我能知道这个目录将存储在哪里吗?
      • @SSS 如果 Android 代码退出,它看起来默认为外部存储目录。如果未安装,则默认为 /data/data/{package name}。
      • 是否可以同时创建多个文件夹?我的意思是,类似 fileSystem.root.getDirectory("data/subfolder", {create: true}, gotDir);。还是我必须先创建数据,然后再创建子文件夹?
      • 无法在任何地方找到文件:(
      【解决方案4】:

      这是工作吗?

      var file = fileSystem.root.getFile("data" + "lockfile.txt", {create: true, exclusive: true});
      

      【讨论】:

      • 它使用这个名称创建文件“datalockfile.txt”
      猜你喜欢
      • 2016-08-04
      • 2013-09-25
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      • 2012-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多