Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Developer
Posts: 466
Registered: ‎04-11-2012
My Device: Bold 9900
My Carrier: Rogers

Overwriting a file using HTML5 on PlayBook

I started a thread on this a while ago but I lost it because no one ever responded. Has anyone been able to edit an exisiting text file using WebWorks? I am able to write and read but not edit or overwrite to a file.

 

Thanks!


Developer for easyDial for BlackBerry Bold & inLink for BlackBerry PlayBook
Find me online via twitter, or on the techfruits.com webpage. Please hit the like button below if what I said helped!
Retired
Posts: 1,382
Registered: ‎07-02-2009
My Device: BlackBerry Bold 9900
My Carrier: Bell

Re: Overwriting a file using HTML5 on PlayBook

Good question.

 

Is the file you are trying to modify one that was created by your application?  Is it located in the /shared folder location on the file system, or in the sandboxed location for your application itself?

 

The QNX file system has read/write/execute permissions for users/groups/other that is similar to Linux.

 

I'm curious if the file in question cannot be edited because the file permissions are restricting this action?

Follow me on Twitter: @n_adam_stanley
-------------------------------------------------------------------------------------------------------------------------
Your app doesn't work? Use BlackBerry remote web inspector to find out why.
Developer
Posts: 466
Registered: ‎04-11-2012
My Device: Bold 9900
My Carrier: Rogers

Re: Overwriting a file using HTML5 on PlayBook

This is the Script I use to write

 

function savefile(someMsg)	{
	var filePath = blackberry.io.dir.appDirs.shared.downloads.path + '/test.txt';

	// What do you want to save in the file?	
	var data_to_save = someMsg ;

	// Make a blob to write to a file from your string
	var blob_to_write = blackberry.utils.stringToBlob(data_to_save);

	// Write the blob to the file 
	blackberry.io.file.saveFile(filePath, blob_to_write);
	alert('Updated');
}

function readfile()	{
// What file do you want to read from? //
var filePath = blackberry.io.dir.appDirs.shared.downloads.path + '/test.txt';

// Read data from the file (asynchronous) //
blackberry.io.file.readFile(filePath, callback_when_read);
}
// If you want to read the data synchronously instead: //
// blackberry.io.file.readFile(filePath, callback_when_read, false);

// The following function (specified in your call to readFile) is called
//  when the file has been read. 
function callback_when_read(filePath, fileData)
{
// Do whatever you want with the file //
// filePath = path of file that was read
// fileData = a blob containing the contents of the file
// (you can name those parameters whatever you want,
//  but you need to have both!)

// You want a string and not a blob?
var myData = blackberry.utils.blobToString(fileData);
alert(myData);
  }

 

 

This is the permissions I used in the config.xml

 

    <rim:permit>access_shared</rim:permit> 
  	</rim:permissions> 
	<content src="index.html"/>
	<feature id="blackberry.app" version="1.0.0" />
	<feature id="blackberry.app.event"  required="true" version="1.0.0.0" /> 
	<feature id="blackberry.invoke" required="true" version="1.0.0"/>
	<feature id="blackberry.invoke.BrowserArguments" required="true" version="1.0.0"/>
	<feature id="blackberry.identity" required="true" version="1.0.0"/>
	<feature id="blackberry.ui.dialog" required="true" version="1.0.0"/>
	<feature id="blackberry.io.dir" required="true" version="1.0.0.0"/>
	<feature id="blackberry.io.file" required="true" version="1.0.0.0"/>
	<feature id="blackberry.utils"/>

 

 

I basically put in all the permissions that I find may or may not be applicable in hopes of trying to fix it.

 

When I run it, if the file does not exist, it can be written into. If it exists an error is returned. The folder being used is the downloads folder on the playbook. There are no issues with reading (if the file exists)


Developer for easyDial for BlackBerry Bold & inLink for BlackBerry PlayBook
Find me online via twitter, or on the techfruits.com webpage. Please hit the like button below if what I said helped!
Developer
Posts: 26
Registered: ‎06-01-2010
My Device: Dev Alpha, PlayBook

Re: Overwriting a file using HTML5 on PlayBook

[ Edited ]

The function blackberry.io.file.saveFile does not permit overwriting files on PlayBook. Thankfully there is an easy way to enable this functionality by editing the function in FileExtension.as. On Windows 64bit computers, the path for the file is likely the following:

 

C:\Program Files (x86)\Research In Motion\BlackBerry WebWorks SDK for TabletOS 2.2.0.5\bbwp\ext\blackberry.io.file\src\Air\File\src\blackberry\io\file\FileExtension.as

 

Once the file is open, take a look at lines 281-283.

//Throw exception if file exists already
if (file.exists && !file.isDirectory)
   throw new Error("File exists at: " + path + ". Please save to a new file.");

Comment out lines 282 and 283 with // and you should be able to now overwrite any file you have access to.

 

I feel like the current code should be removed as it is a reasonable assumption that existing files should be overwritable.

___________________________________________________________
My latest Blackberry 10 app Audiobooks! Smiley Happy

My latest PlayBook app Photojam!

Find me on Twitter: GoGibbons