WinJS uploads local json file

I hit my head about it.

I cannot find a way to open a simple json file from a subfolder in my WinJS application.

I tried Ajax and WinJS.xhr, both to no avail.

I also considered opening a file in an “old fashioned” way with something like File.Open in .NET, but I could not find anything except WinJS.Application.local.readText, which I tried with both an absolute and relative path.

I am at the end of my rope, does anyone have a working fragment that you can share?

+5
source share
2 answers

You can link to files in the application package using URLs in the form:

ms-appx:///data/data.json

( , / - , )

, JSON, Windows.Storage. - StorageFile, , JSON. , :

var url = new Windows.Foundation.Uri("ms-appx:///data/data.json");
Windows.Storage.StorageFile.getFileFromApplicationUriAsync(url).then(function (file) {
    Windows.Storage.FileIO.readTextAsync(file).then(function (text) {
        var parsedObject = JSON.parse(text);
        // do something with object
    });
});

, , FileIO . , JSON. , , :

var url = new Windows.Foundation.Uri("ms-appx:///data/data.json");
Windows.Storage.StorageFile.getFileFromApplicationUriAsync(url).then(function (file) {
    Windows.Storage.FileIO.readLinesAsync(file).then(function (lines) {
        lines.forEach(function (line) {
            var parsedObject = JSON.parse(line);
            // do something with object
        });
    });
});

, FileIO.readLinesAsync , JSON.

+10

,

WinJS.xhr({ url: "data/mydata.txt" }).then(...)

. , mydata.txt data​​p >

+4

All Articles