locked
How to append text to a file in a storage folder?

    Question

  • I have following function. I want to append JSON text to my file. This code gives error "Access denied". How can I append to the file in the application folder?

    function

    saveData(filePath) {

           

    varuri = newWindows.Foundation.Uri(filePath);

           

    /*return Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri)

              .done(function (file) {

                  var userData = JSON.stringify(newUser);

                  console.log(file);

                  console.log(newUser);

                  return Windows.Storage.FileIO.appendTextAsync(file, userData);

              });*/


           

    returnWindows.Storage.StorageFile.getFileFromApplicationUriAsync(uri)

              .then(

    function(file) {

                 

    //console.log(file);


                 

    varuserData = JSON.stringify(newUser);

                  console.log(userData)

                 

    returnWindows.Storage.FileIO.appendTextAsync(file, userData);

                 

              }).done(

    function(e) {

                  console.log(e.message);

              })    }

       


    sonal

    Sunday, April 27, 2014 7:34 PM

Answers

  • Your package location is read-only, so anything referenced from ms-appx or ms-appx-web is read-only, hence your access denied exception. If you want to modify a package file, copy it first to your local appdata, where you can reference it with ms-appdata:///local/ URIs, then use that file in your app.
    • Marked as answer by SonalMac Monday, April 28, 2014 6:31 PM
    Monday, April 28, 2014 4:29 PM

All replies

  • Hi SonalMac,

    Well, access denied, may I know what your file path is? Take a look at the File access and permissionsif your file path is not in the app access list. 

    And do not forget to declare capabilities in the app manifest if your file is in Document, Picture or etc, folders. Probably you need to add "ms-appx:///" or "ms-appdata://" before your path if you are tying to access the file inside app package or in local folder.

    BTW, Your code seems correct, I cannot find any mistake from it.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Monday, April 28, 2014 6:55 AM
    Moderator
  • Thanks for the reply.

    My filepath is :

    varfilePath = "ms-appx:///data/Scientist_JSON.txt"; //because I want to add data in this file, once I load some data from it. It's in the app package.


    sonal

    Monday, April 28, 2014 7:05 AM
  • Another possibility: your txt file is used by other application or you open the file without close it in your previous code.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, April 28, 2014 7:43 AM
    Moderator
  • I'm using this file to load a listview. And above code is the first time to access the file. After that it will load the data in listview.  I'm trying to zip and upload this project to OneDrive. Pls. take a look..

    If I read data like following, it definitely gives back all the data  from the file in above function.

    return Windows.Storage.FileIO.readTextAsync(file);


    sonal


    • Edited by SonalMac Tuesday, April 29, 2014 5:56 AM
    Monday, April 28, 2014 3:23 PM
  • Your package location is read-only, so anything referenced from ms-appx or ms-appx-web is read-only, hence your access denied exception. If you want to modify a package file, copy it first to your local appdata, where you can reference it with ms-appdata:///local/ URIs, then use that file in your app.
    • Marked as answer by SonalMac Monday, April 28, 2014 6:31 PM
    Monday, April 28, 2014 4:29 PM
  •  Thanks a lot. I tried as your suggestion. And now I have problem giving datasource to my listview.

    WinJS.xhr({ url:

    "ms-appdata://local/Scientist1_JSON.txt"}).then(function(xhr) {

           

    varitems = JSON.parse(xhr.responseText);

    This gives error of "Invalid argument". 


    sonal


    It's my mistake: It should be ms-appdata:///.....  Thanks again!!!!!
    • Edited by SonalMac Monday, April 28, 2014 6:30 PM
    Monday, April 28, 2014 6:07 PM
  • You probably need to load the JSON from the file, parse it into a JavaScript object, add you new values, then stringify and save. Or you have to manually parse the string to find the correct insertion point for your new data as a simple append won't work.
    Monday, April 28, 2014 9:26 PM
  • Looks like this new data will not work to load a list view data source. When an app runs, it loads data available in the app package into memory, and once that data is in memory, there is no chance to append another data to the file in local folder. IS that so? or Is there a workaround?

    sonal

    Tuesday, April 29, 2014 5:47 PM
  • That all depends on the kind of data source you're using for the ListView and how you manage the insertion of new items into that source. If you're parsing the JSON and creating items in a WinJS.Binding.List, for example, then you'll need to separately add new items that you also add to the JSON to keep them in sync. This won't happen automatically when you update a file unless you write a file-based data source that monitors changes to the file as well.

    If you need more help on this particular issue, I recommend starting a new forum thread so others can see that you're asking about ListView now.

    Tuesday, April 29, 2014 8:28 PM