my name is xyz
my name is xyz

Reputation: 349

creating files in tizen

I am trying to create new file in tizen. My code is

var dir;
var newDir = dir.createDirectory("vij");

But Its getting Error like

TypeError: 'undefined' is not an object (evaluating 'dir.createDirectory')

I have tried the same example which is given in the Tizen documentation. Please give an idea

Upvotes: 3

Views: 3939

Answers (5)

Byonggon Chun
Byonggon Chun

Reputation: 45

You need resolve file or directory object from tizen.filesystem.resolve API befor using directory object.

In your code your dir object is just contain empty string.

So, First get File(file&dir) object from tizen.filesystem.resolve API.

below is tizen.filesystem.resolve api, you can get file object on FileSuccessCallback onsuccess method.

void resolve(DOMString location, FileSuccessCallback onsuccess, optional ErrorCallback? onerror, optional FileMode? mode);

like below code.

tizen.filesystem.resolve(
   'images',
   function(dir) {
     //do something what you want
     console.log("Mount point Name is " +  dir.path);
   }, function(e) {
     console.log("Error: " + e.message);
   }, "r"
 );

There is helpfull tutorial and example code on API Reference.

FileSystem Tutorial https://developer.tizen.org/development/tutorials/web-application/tizen-features/base/filesystem#create

Filesystem API Reference https://developer.tizen.org/dev-guide/latest/org.tizen.web.apireference/html/device_api/mobile/tizen/filesystem.html

And Below is virtual roots table

images - the location for images
videos - the location for videos
music - the location for sounds
documents - the location for documents
downloads - the location for downloaded items
ringtones - the location for ringtones (read-only location)
camera - the location for the pictures and videos taken by a device (supported since Tizen 2.3)
wgt-package - the location for widget package which is read-only
wgt-private - the location for a widget's private storage
wgt-private-tmp - the location for a widget's private volatile storage

Upvotes: 2

lahir
lahir

Reputation: 21

this is how you add an file in the tizen web application:

Create a File:

Creates a empty new file in a specified location that is relative to the directory. "File createFile(DOMString relativeFilePath);"

Example:

tizen.filesystem.resolve(
   absolute_path, 
   function(dir){
    dir.createFile(<filename>);
   }, function(e) {
     console.log("Error" + e.message);
   }, "rw"
);

More info can be found in this link below: http://howdoudoittheeasiestway.blogspot.in/2015/02/writing-and-reading-from-file-system.html

Upvotes: 2

Ender
Ender

Reputation: 158

First of all, you need 2 privilages in config.xml to write to files:
<tizen:privilege name="http://tizen.org/privilege/filesystem.write"/> <tizen:privilege name="http://tizen.org/privilege/filesystem.read"/>
Can be added manually, or using "previlages" tab. After that, you shpuld use Filesystem API methods - createDirectory

tizen.filesystem.resolve( 'images', function(dir) { var dir; //Directory object obtained from filesystem API var newDir = dir.createDirectory("newDir"); console.log("Mount point Name is " + dir.path); }, function(e) { console.log("Error: " + e.message); }, "rw" ); As a result you will have your folder in /opt/usr/media/Images/

Upvotes: 2

piotrowskim
piotrowskim

Reputation: 61

Just declare dir like so:

var dir = tizen.filesystem;

Upvotes: 4

Eduard Florinescu
Eduard Florinescu

Reputation: 17541

Because of var dir; in your code were you practically declare an undefined dir variable you get that error because dir will be undefined.

If you check the API in Documentation this is the way to do it:

 var documentsDir;
 function onsuccess(files) {
   for(var i = 0; i < files.length; i++) {
     console.log("File Name is " + files[i].name); // displays file name
   }

   var testFile = documentsDir.createFile("test.txt");
   if (testFile != null) {
     testFile.openStream(
         "w",
         function(fs){
           fs.write("HelloWorld");
           fs.close();
         }, function(e){
           console.log("Error " + e.message);
         }, "UTF-8"
     );
   }
 }

 function onerror(error) {
   console.log("The error " + error.message + " occurred when listing the files in the selected folder");
 }

 tizen.filesystem.resolve(
     'documents',
     function(dir){
       documentsDir = dir; dir.listFiles(onsuccess,onerror);
     }, function(e) {
       console.log("Error" + e.message);
     }, "rw"
 );

The two bellow shown examples that you find in the documentation make sense just in the context above were tizen.filesystem.resolve is called.

var newDir = dir.createDirectory("newDir");
var anotherNewDir = dir.createDirectory("newDir1/subNewDir1");     

so if you want to create a file you do the above code (first one entirely) were the file is create in the onsuccess callback of listFiles, if you want to create a directory yo need to do this:

tizen.filesystem.resolve(
       'documents', 
       function(dir){
        var newDir = dir.createDirectory("vij");
       }, function(e){
         console.log("Error" + e.message);
       }, "rw"
     );

Upvotes: 2

Related Questions