Windows 8 app development requires a file access and i'm using html with javascript. I read the msdn articles and other questions posted here. But every answer points to accessing file read via user input in html file. But I want to access the file contents like using java FileIO. My operation would be requiring reading first line from a file into a variable...Kindly help me on this.
Asked
Active
Viewed 220 times
0
-
Duplicate of http://stackoverflow.com/questions/14446447/javascript-read-local-text-file ? Using file:// for local files. – Devon Bessemer Dec 27 '13 at 18:15
-
you can use a FileReader to grab small chunks of the file. you'll have to find your own lines within the chunks however. – dandavis Dec 27 '13 at 18:47
-
@dandavis can u explain it with example?? – user1981245 Dec 27 '13 at 18:54
-
1FileReader is a blob, which has a slice() method that can return a new blob which you give to another filereader to get the segment value. – dandavis Dec 28 '13 at 07:23
1 Answers
0
I'd assume the link posted in similar question (here https://stackoverflow.com/a/13131746/173100) helps. Just note that you can't read an arbitrary file from the file system, the allowed locations include the app's install location, app's local data folder and a bunch of predefined folders described in the linked MSDN article (http://msdn.microsoft.com/en-us/library/windows/apps/hh967755.aspx). Most likely you're interested in http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.storagefile.getfilefromapplicationuriasync.aspx .
In practice, you could read the first line of the file foo.txt
located in your app's install directory (= in your app's package) as follows:
Windows.Storage.StorageFile.getFileFromApplicationUriAsync("ms-appx:///foo.txt").done(
function(file) {
// Assuming file has content here, better error handling probably needed
var lines = file.split("\n");
var firstLine = lines[0];
// Do something with the firstLine variable
},
function(error) {
// Error occurred, handle it here
}
);