How can I write an html file to the WWW directory?

493 views
Skip to first unread message

Jason Whitmer

unread,
Feb 11, 2012, 9:54:15 PM2/11/12
to phonegap
In iOS, I'm trying to write an html file to the WWW directory, so I
can link to it in my app. Using the same code I can write to the
Documents folder without an issue. How can I write an html file to the
WWW directory?

Thank you,
Jason

Here is my test code:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
alert('in onDeviceReady');
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS,
fail);
}

function gotFS(fileSystem) {
alert('in gotFS');

// var rootPath = fileSystem.root.fullPath; // THIS LINE WORKS
AND WRITES TO DOCUMENTS
var rootPath =
fileSystem.root.fullPath.substring(0,fileSystem.root.fullPath.lastIndexOf('/'))
+ '/Lightningbug.app/www/'; // THIS LINE DOES NOT WORK

fileSystem.root.getFile(rootPath + 'test.html', { create: true },
gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
alert('in gotFileEntry');
fileEntry.createWriter(gotFileWriter, fail);
}

function gotFileWriter(writer) {
writer.onwrite = function(evt) {
alert("write success");
};
writer.write("some sample text");
}

function fail(error) {
alert('in failure: '+error.code);
}

Simon MacDonald

unread,
Feb 12, 2012, 8:38:57 AM2/12/12
to phon...@googlegroups.com
You can't. The www folder is not part of the file system.

Simon
> --
> You received this message because you are subscribed to the Google
> Groups "phonegap" group.
> To post to this group, send email to phon...@googlegroups.com
> To unsubscribe from this group, send email to
> phonegap+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/phonegap?hl=en?hl=en
>
> For more info on PhoneGap or to download the code go to www.phonegap.com
>

--
Simon Mac Donald
http://hi.im/simonmacdonald

Kerri Shotts

unread,
Feb 12, 2012, 6:09:27 PM2/12/12
to phon...@googlegroups.com
As Simon said, you can't; www isn't accessible in this fashion. Is there a reason you need to write into it? Why not have code that writes what you need to DOCUMENTS and then reads it back? (Especially if this is to be considered persistent in any way; the app folder can be considered volatile -- that is, an upgrade is free to nuke it completely.)

Jason Whitmer

unread,
Feb 12, 2012, 6:37:35 PM2/12/12
to phonegap
I want to create an html file and load/display it in phonegap. So I
could write to the Documents, if there is a way to write a link to go
to the Documents folder. in my main phonegap app I want to create a
link to go to my generated html file, like... <a href="file:///
Documents/generated.html">link</a> Is something like that possible?

Thank you,
Jason

Kerri Shotts

unread,
Feb 12, 2012, 6:53:07 PM2/12/12
to phon...@googlegroups.com
Not as written, no. What you'd have to do is have a javascript function read the contents of your file and drop the results into an HTML element -- much like using regular AJAX. Doing that should be fine. Your href could then be href="javascript:your_function();" and let your_function() process the document. Not perfect, but as WWW is unavailable to you to write to, I /suspect/ that DOCUMENTS is equally unavailable to LINK to. (That said, try it. I might be wrong.)

@RandyMcMillan

unread,
Feb 13, 2012, 11:52:27 AM2/13/12
to phonegap
https://github.com/RandyMcMillan/SenchaTouch

I am still creating a more robust demo for filewriter but this source
will give you some insight.

Aater Suleman

unread,
Feb 13, 2012, 5:43:34 PM2/13/12
to phonegap
Kerri is absolutely right. I struggled with this 2 months ago for 2
days.

On Feb 13, 10:52 am, "@RandyMcMillan" <randy.lee.mcmil...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages