Create a Directory in PhoneGap Application

纵饮孤独 提交于 2019-12-17 17:56:28

问题


Trying to figure out how to create a directory on the file system using PhoneGap.

I want to create a directory for my PhoneGap application, so I can store images the user creates there and load them back up in the app.


回答1:


This is how you do it :

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onRequestFileSystemSuccess, null); 

function onRequestFileSystemSuccess(fileSystem) { 
        var entry=fileSystem.root; 
        entry.getDirectory("example", {create: true, exclusive: false}, onGetDirectorySuccess, onGetDirectoryFail); 
} 

function onGetDirectorySuccess(dir) { 
      console.log("Created dir "+dir.name); 
} 

function onGetDirectoryFail(error) { 
     console.log("Error creating directory "+error.code); 
} 

On iOS, this script will create a directory named 'example' in Applications/YOUR_APP/Documents/




回答2:


This code works for me (based on version 7 Cordova documentation - https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/index.html):

window.resolveLocalFileSystemURL('cdvfile://localhost/persistent/',
  function fileEntryCallback(fileEntry) {
    fileEntry.getDirectory('my_directory', { create: true });
  }
);

If the code above doesn't work, you may need to reinstall the File and File Transfer plugins. A command like console.log(window.resolveLocalFileSystemUrl) is one way to test that the plugins are working.

Also, if you're using Content Security Policy (CSP) be sure your default-src allows cdvfile: paths:

default-src cdvfile:



来源:https://stackoverflow.com/questions/9343204/create-a-directory-in-phonegap-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!