Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading files in js from local windows store folder

My problem is that I can't read my json file that I'm creating in c#. I need my longitude and latitude values in my js. I need these for creating a google maps webview. My js can't find/read this file. I'm not sure if this is the correct way of reading/making the json file.

With this I create my JSON file. The beginPositie has 2 variables: longitude and latitude.

        public async Task Serialize(Coordinate beginPositie)
        {
        string json = JsonConvert.SerializeObject(beginPositie);

        StorageFolder localFolder = ApplicationData.Current.LocalFolder;

        StorageFile MarkersFile = await localFolder.CreateFileAsync("markers.json", CreationCollisionOption.ReplaceExisting);

        using (IRandomAccessStream textStream = await MarkersFile.OpenAsync(FileAccessMode.ReadWrite))
        {
            using (DataWriter textWriter = new DataWriter(textStream))
            {
                textWriter.WriteString(json);
                await textWriter.StoreAsync();
            }
        }
    }

This is my function for reading the JSON file in JS. The "Windows" can't be found and I don't know the reason for that. I have already included the scripts, installed the extension SDK for js but for a reason I can't add the reference to this SDK.

function getJSON() {
//var uri = new Windows.Foundation.Uri('ms-appdata:///local/markers.json');
//json = Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri);
$.ajax({
    url: "ms-appdata:///local/markers.json",
    success: function (data) {
        json = JSON.parse(data);
    }
});

}

like image 594
user3005561 Avatar asked Nov 02 '22 10:11

user3005561


1 Answers

Check out the localFolder property of the ApplicationData class. This code should retrieve the file data you're looking for:

Windows.Storage.ApplicationData.current.localFolder.getFileAsync("markers.json").done(
 function (file) {
    Windows.Storage.FileIO.readTextAsync(file).done(
       function (fileContent) {
          //'fileContent' contains your JSON data as a string
       },
       function (error) {
          //file couldn't be read - handle the error
       });
 },
 function (error) {
    //file not found, handle the error
 });
like image 69
Steve Danner Avatar answered Nov 08 '22 05:11

Steve Danner