locked
Writing to a .dat file with javascript

    Question

  • Hi,

    I have looked in many places on the web on how to write to .dat file with javascript but haven't found a solution. For example, say that you want write some english study to a .dat file. Here is the code so far, the write function is near the bottom. The problem with the code is that whenever I press the button that says 'Write English Study', the app always crashes, or at least closes. Note that the html and javascript is all in one.

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script src="/study/study.js"></script>
        <title></title>
    </head>
    <body>
        <h1>Study</h1>
        <p>The devotion of time and attention to gaining knowledge of an
            academic subject, especially by means of books.</p>
        <h2>English</h2>
        <p>You will need to create an English Study File to save your study.</p>
        <button id="createEnglish" onclick="createEnglish()">Create</button>
        <script type="text/javascript">
        function createEnglish() {
            Windows.Storage.KnownFolders.picturesLibrary.createFileAsync("English Study.dat", Windows.Storage.CreationCollisionOption.failIfExists).done(
               function message() {
                   (new Windows.UI.Popups.MessageDialog("The file English Study.dat was created successfully", "Message:")).showAsync().done();
               },
               function (error) {
                   (new Windows.UI.Popups.MessageDialog("The following error occured: " + error, "Message:")).showAsync().done();
               });
        }
        </script>
    
        <h3>You can type in your English Study below:</h3>
        <textarea rows="20" cols="100" id="englishArea" name="englishArea"></textarea>
        <button id="writeEnglishButton" onclick="writeEnglish()">Write English Study</button>
    
        
    
    <!--This is where the write to .dat file function is-->
    
    
    <script type="text/javascript">
            function writeEnglish() {
    
                var English_Study_File = Windows.Storage.KnownFolders.picturesLibrary.getFileAsync("English Study.dat");
    
                if (English_Study_File = true) {
    
                    var textAreaEnglish = document.getElementById("englishArea");
                    var englishContent = textAreaEnglish.innerText;
                    var englishFile = Windows.Storage.KnownFolders.picturesLibrary("English Study.dat");
    
                    if (englishContent !== "") {
                        Windows.Storage.FileIO.appendTextAsync(englishFile, englishContent).done(function () {
                            (new Windows.UI.Popups.MessageDialog("The following text was written to 'English Study.dat':\n" + englishContent, "Message:")).showAsync().done();
                        },
    
                        function (error) {
                            (new Windows.UI.Popups.MessageDialog("The file 'English Study.dat' does not exist; please create using the button above.", "Message:")).showAsync().done();
                        });
    
                    } else {
                        (new Windows.UI.Popups.MessageDialog("The text box is empty, please write something and then click 'Write English Study' again.", "Message:")).showAsync().done();
                    }
                }
            }
        </script>
    </body>
    </html>

    Thanks in advance.


    Saturday, August 23, 2014 1:37 AM

Answers

  • What error do you get and where?

    Are you declaring the pictureslibrary capability in your manifest?

    This line looks wrong:

      var englishFile = Windows.Storage.KnownFolders.picturesLibrary("English Study.dat");
    
    

    KnownFolders.picturesLibrary is a StorageFolder. To get a file from it call its getFileAsync method

    • Marked as answer by deguppy Wednesday, August 27, 2014 8:05 AM
    Saturday, August 23, 2014 7:02 AM
    Moderator

All replies

  • What error do you get and where?

    Are you declaring the pictureslibrary capability in your manifest?

    This line looks wrong:

      var englishFile = Windows.Storage.KnownFolders.picturesLibrary("English Study.dat");
    
    

    KnownFolders.picturesLibrary is a StorageFolder. To get a file from it call its getFileAsync method

    • Marked as answer by deguppy Wednesday, August 27, 2014 8:05 AM
    Saturday, August 23, 2014 7:02 AM
    Moderator
  • I didn't get any error, the app just terminated with exit code 0x0. I will see if getfileasync will help.
    Saturday, August 23, 2014 12:18 PM