06-28-2012 03:03 PM
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!
07-04-2012 02:50 PM
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?
07-05-2012 05:15 PM
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)
09-20-2012 03:09 PM - edited 09-20-2012 03:11 PM
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\s
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.