cordova-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sergey Grebnov (Akvelon)" <v-seg...@microsoft.com>
Subject RE: Can't get cordova 2.2 File api to work.
Date Mon, 19 Nov 2012 13:55:24 GMT
Hi James,

I was able to get your example working with a few corrections below:


1. onLoad function is not called; you can call it as <body onload="onLoad()">  but it
is much simpler just to place document.addEventListener inline to main script. 


        //function onLoad() {

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

        //}

2. Actually all files from Cordova www folder are located in the following folder in isolated
storage: app/www; so you should use the following path when accessing readme.txt (even if
it is located in the same folder as index.html):

fileSystem.root.getFile("app/www/readme.txt", null, gotFileEntry, fail);


Hope this helps!  Below is a full version of index.html which works for me.

<!DOCTYPE html>
<html>
<head>
    <title>FileReader Example</title>

    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no;"
/>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>


    <script type="text/javascript" charset="utf-8" src="cordova-2.2.0.js"></script>

    <script type="text/javascript" charset="utf-8">


        // provide our own console if it does not exist, huge dev aid!

        window.console = { log: function (str) { window.external.Notify(str); } };

        // Wait for Cordova to load

        //function onLoad() {

            console.log("function onLoad");
            document.addEventListener("deviceready", onDeviceReady, false);

        //}

        

        // Cordova is ready        

        function onDeviceReady() {

            console.log("function onDeviceReady");

            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

        }

        function gotFS(fileSystem) {
            console.log("function gotFS");
            fileSystem.root.getFile("app/www/readme.txt", null, gotFileEntry, fail);

        }

        function gotFileEntry(fileEntry) {
            console.log("function gotFileEntry");
            fileEntry.file(gotFile, fail);

        }

        function gotFile(file) {
            console.log("function gotFile");

            //readDataUrl(file);

            readAsText(file);

        }

        function readDataUrl(file) {

            var reader = new FileReader();

            reader.onloadend = function (evt) {

                console.log("Read as data URL");

                console.log(evt.target.result);

            };

            reader.readAsDataURL(file);

        }

        function readAsText(file) {

            var reader = new FileReader();

            reader.onloadend = function (evt) {

                console.log("Read as text");

                console.log(evt.target.result);

            };

            reader.readAsText(file);

        }

        function fail(evt) {
            console.log("fail");
            console.log(JSON.stringify(evt));

        }

    </script>

</head>

<body>

    <h1>Example</h1>

    <p>Read File</p>

</body>

</html>

-----Original Message-----
From: James M Earle III [mailto:jmearle@mindspring.com] 
Sent: Friday, November 16, 2012 9:53 AM
To: dev@cordova.apache.org
Subject: Can't get cordova 2.2 File api to work.

Hey,

 

I am trying to open and write/read a file on WinPhone 7.10.xxx or 7.5 using File api.

 

Here is the app in index.html and error following.

 

Says it can't load content which makes me think html is malformed but can't find any.

 

Can anybody tell me what is wrong or try it out?

 

 

Index.html

<!DOCTYPE html>

<html>

<head>

    <title>FileReader Example</title>

    <script type="text/javascript" charset="utf-8"
src="cordova-2.2.0.js"></script>

    <script type="text/javascript" charset="utf-8">

    // Wait for Cordova to load

    function onLoad() {

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

    }

    // Cordova is ready        

    function onDeviceReady() {

        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

    }

    function gotFS(fileSystem) {

        fileSystem.root.getFile("readme.txt", null, gotFileEntry, fail);

    }

    function gotFileEntry(fileEntry) {

        fileEntry.file(gotFile, fail);

    }

    function gotFile(file) {

        readDataUrl(file);

        readAsText(file);

    }

    function readDataUrl(file) {

        var reader = new FileReader();

        reader.onloadend = function (evt) {

            console.log("Read as data URL");

            console.log(evt.target.result);

        };

        reader.readAsDataURL(file);

    }

    function readAsText(file) {

        var reader = new FileReader();

        reader.onloadend = function (evt) {

            console.log("Read as text");

            console.log(evt.target.result);

        };

        reader.readAsText(file);

    }

    function fail(evt) {

        console.log(evt.target.error.code);

    }    

    </script>

</head>

<body>

    <h1>

        Example</h1>

    <p>

        Read File</p>

</body>

</html>

 

 

And console output from running on emulator in Win7 and VS2010 Pro with phone sdks

 

'UI Task' (Managed): Loaded 'mscorlib.dll'

'UI Task' (Managed): Loaded 'System.Windows.RuntimeHost.dll'

'UI Task' (Managed): Loaded 'System.dll'

'UI Task' (Managed): Loaded 'System.Windows.dll'

'UI Task' (Managed): Loaded 'System.Net.dll'

'UI Task' (Managed): Loaded 'System.Core.dll'

'UI Task' (Managed): Loaded 'System.Xml.dll'

'UI Task' (Managed): Loaded
'\Applications\Install\70C414C4-F9F3-4CCE-800F-4219EC2A8C2B\Install\CordovaA
ppProj_2.2.01.dll', Symbols loaded.

'UI Task' (Managed): Loaded 'Microsoft.Phone.dll'

'UI Task' (Managed): Loaded 'Microsoft.Phone.Interop.dll'

'UI Task' (Managed): Loaded
'\Applications\Install\70C414C4-F9F3-4CCE-800F-4219EC2A8C2B\Install\WP7Cordo
vaClassLib.dll', Symbols loaded.

'UI Task' (Managed): Loaded 'System.Xml.Linq.dll'

'UI Task' (Managed): Loaded 'System.Runtime.Serialization.dll'

'UI Task' (Managed): Loaded 'System.SR.dll'

A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll

A first chance exception of type
'System.IO.IsolatedStorage.IsolatedStorageException' occurred in mscorlib.dll

Updating IsolatedStorage for APP:DeviceID ::
7a5ef5bd-09b6-4f09-b5fe-6786e7188b5a

INFO: Creating Directory :: /app/www

INFO: Writing data for /app/www\cordova-2.2.0.js and length = 205469

INFO: Creating Directory :: /app/www\img

INFO: Writing data for /app/www\img\logo.png and length = 21814

INFO: Creating Directory :: /app/www\js

INFO: Writing data for /app/www\js\index.js and length = 1914

INFO: Writing data for /app/www\ProjHelp.txt and length = 1947

INFO: Creating Directory :: /app/www\css

INFO: Writing data for /app/www\css\index.css and length = 3719

INFO: Writing data for /app/www\index.html and length = 1532

GapBrowser_Navigated :: /app/www/index.html

'UI Task' (Managed): Loaded 'System.ServiceModel.Web.dll'

'UI Task' (Managed): Loaded 'System.ServiceModel.dll'

The thread '<No Name>' (0xe6900fe) has exited with code 0 (0x0).

Error:"Unable to get value of the property 'content': object is null or undefined file:x-wmapp1:/app/www/index.html
Line:1"

The thread '<No Name>' (0xe7300da) has exited with code 0 (0x0).

A first chance exception of type 'System.SystemException' occurred in Microsoft.Phone.Interop.dll

The thread '<No Name>' (0xf63010e) has exited with code 0 (0x0).

 

 


Mime
View raw message