最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to create a separate directory for cordova app and storing data in it? - Stack Overflow

programmeradmin5浏览0评论

I want to create a specific directory for my cordova project. I have no idea what to do for that. I refered so may links like, How to move file to app directory cordova

cordova, android app how to create subfolder and someother links. But it is not clear whether they are using any cordova plugins for this or we can do with pure javascript. These are not working for me. Please suggest if any plugins or functions are available.

Thanks.

I want to create a specific directory for my cordova project. I have no idea what to do for that. I refered so may links like, How to move file to app directory cordova

cordova, android app how to create subfolder and someother links. But it is not clear whether they are using any cordova plugins for this or we can do with pure javascript. These are not working for me. Please suggest if any plugins or functions are available.

Thanks.

Share Improve this question edited May 23, 2017 at 12:01 CommunityBot 11 silver badge asked Jan 28, 2016 at 8:37 Harish KommuriHarish Kommuri 2,8641 gold badge23 silver badges28 bronze badges 1
  • Try showing some code – ste-fu Commented Jan 28, 2016 at 8:45
Add a ment  | 

1 Answer 1

Reset to default 5

You need to use this plugin: https://www.npmjs./package/cordova-plugin-file

I don't know what version of cordova do you use and if this plugin was updated, but when I use it, it doesn't work with WindowsPhone. Ok for Android and iOS.

To get file system:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, downloadFile, fileSystemFail);

Donwload file function:

downloadFile: function(fileSystem){
    // your code
}

To create a directory:

var directoryEntry = fileSystem.root;
var folderName = "Folder";

directoryEntry.getDirectory(folderName, { create: true, exclusive: false }, onDirectorySuccess, onDirectoryFail);

Where onDirectorySuccess and onDirectoryFail are functions like:

onDirectorySuccess: function(parent){
    console.log(parent);
},
onDirectoryFail: function(error){
    console.log("Unable to create new directory: " + error.code);
}

To get path of file in the directory:

var filePath = directoryEntry.toURL() + "/" + folderName + "/" + fileName;

And to get file:

directoryEntry.getFile(folderName + "/" + fileName, { create: true, exclusive: false }, onFileSuccess, onFileFail);

To delete a file first you need to get it:

directoryEntry.getFile(folderName + "/" + fileName, { create: true, exclusive: false }, onFileSuccessRemove, onFileFail);

Then in success function:

function onFileSuccessRemove(entry) {
    entry.remove();
}

My application was with file download, so this is code to save a file in a directory:

var fileTransfer = new FileTransfer();
fileTransfer.download(URL, filePath, downloadComplete, downloadFail, true);

It's not your case but I hope it helps.

发布评论

评论列表(0)

  1. 暂无评论